Cbas
Cbas

Reputation: 6213

Can't remove image from UICollectionViewCell

I have a UICollectionView where each cell has a UIImageView object. When a new cell is presented, the image must be loaded from the web, so I want the cell to be black while the image is loading.

I have implemented this, but instead of starting black, the cells start on a random previous image and transition to the new one.

Here is my code:

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

  GalleryCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kGalleryCellIdentifier forIndexPath:indexPath];
  cell.backgroundColor = [UIColor black];
  cell.tag = indexPath.row;
  cell.imageView.image = nil;

  [Util fetchPhoto:urls[indexPath.row] completion:^(UIImage* image){
    if (cell.tag == indexPath.row){
      cell.imageView.image = image;
    }
  }];
}

why doesn't cell.imageView.image = nil clear the image? How can I get the cell to reset to a black view every time?

Upvotes: 1

Views: 403

Answers (1)

MDB983
MDB983

Reputation: 2454

Try implementing the following in your UICollectionViewCell:

  -(void)prepareForReuse{
      [super prepareForReuse];

      // Reset here back to default values.
  }

Upvotes: 2

Related Questions