cellForItemAtIndexPath正在更改多个单元格何时应该更改一个

时间:2014-12-06 00:10:57

标签: ios uicollectionview uicollectionviewcell

我可能不理解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上执行

知道我哪里出错了?

感谢。

0 个答案:

没有答案