Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cellForItemAtIndexPath正在更改多个单元格,此时应更改一个单元格_Ios_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios cellForItemAtIndexPath正在更改多个单元格,此时应更改一个单元格

Ios cellForItemAtIndexPath正在更改多个单元格,此时应更改一个单元格,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我可能不理解UICollectionView重用单元格的行为,但有人能告诉我为什么两个单元格在cellForItemAtIndexPath中的属性发生了更改,而它应该是一个单元格 这是我的密码 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoCollectionViewCell *

我可能不理解UICollectionView重用单元格的行为,但有人能告诉我为什么两个单元格在cellForItemAtIndexPath中的属性发生了更改,而它应该是一个单元格

这是我的密码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

  PhotoCollectionViewCell *cell = (PhotoCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];

  if (![[self.userPhotos objectAtIndex:indexPath.row] isEqualToString:@""]){
    NSString *photoUrl = [self.userPhotos objectAtIndex:indexPath.row];

    NSURL *imageURL = [NSURL URLWithString: photoUrl];
    NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageURL];
   [cell.photoImageView setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:@"anImage"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

      cell.photoImageView.image = image;
      cell.photoImageView.frame = CGRectMake(0, 0, 100.0f,100.0f);
      cell.photoImageView.contentMode = UIViewContentModeScaleAspectFit;

      [cell.deleteImageView setFrame:CGRectMake(-4.0f,-4.0f,28.0f,28.0f)];
      cell.deleteImageView.layer.cornerRadius = cell.deleteImageView.frame.size.width / 2;
      [cell.deleteImageView setAlpha:0.8f];
      cell.deleteImageView.layer.borderWidth = 1.0f;
      cell.deleteImageView.layer.borderColor = [UIColor redColor].CGColor;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
      NSLog(@"%@",[error localizedDescription]);
    }];
  }else{
    cell.photoImageView.image = [UIImage imageNamed:@"camera"];
    cell.photoImageView.layer.borderWidth = 1.0f;
    cell.photoImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    NSLog(@"setting alpha & hiding delete");
    cell.photoImageView.alpha = 0.4f;
    [cell.deleteImageView setHidden:YES];
  }
else部分中的代码正在单元格2和6上执行,例如,当它应该是6时。虽然日志上说它是在6点执行的

知道我哪里出错了吗


谢谢。

只是想检查一下:单元格2一开始看起来正常吗,只有当你向上滚动时才像单元格6?是的。很好,当我滚动时,它似乎是从一个回收的单元格中重新绘制的,即修改后的单元格。您在PhotoCollectionViewCell类中实现了prepareForReuse吗?当集合视图重用单元格时,它将被调用,这是您重置所有内容的机会。查看您的代码,可能是图幅/图层/字母/隐藏。。。必须以某种方式清理重复使用的单元格,无论是在PrepareForuse中还是在cellForRowAtIndexPath中。否则将继续显示旧数据。谢谢!现在可以了。