Rameez Hussain Rameez Hussain - 14 days ago 4
iOS Question

UICollectionView repeats cell

I have a

UICollectionView
where I display a grid of images downloaded from the Internet. I am using
SDWebImage
to load the images. The problem I am facing is that, when I scroll through the
UICollectionView
, sometimes the cells repeat themselves, displaying the same image. But when the cell is scroll out of view and then brought back, it has the right image set.

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

NSString *CellIdentifier = @"Gallery_Cell";

GalleryCell *cell;

if (cell==nil) {
cell= (GalleryCell *)[self.flowCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

ImageItem *dets = [self.imageList objectAtIndex:indexPath.row];

NSURL *mainImageURL = [NSURL URLWithString:dets.smallImageURL];

cell.image.contentMode = UIViewContentModeScaleAspectFill;
cell.image.clipsToBounds = YES;

[cell.image setImageWithURL:mainImageURL placeholderImage:nil];

}

return cell;

}


Has this happened to anyone else? Would highly appreciate any pointers.

Answer

I ended up using “didEndDisplayingCell” method of UICollectionView and ending the current Image download and setting the image to nil. This worked perfectly and there was no more “shuffling” or repetition of images! :)