Rameez Hussain
Rameez Hussain

Reputation: 6764

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.

Upvotes: 8

Views: 6892

Answers (6)

Irfan Khatik
Irfan Khatik

Reputation: 146

-(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 setImage:[UIImage new]];

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

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

}

return cell;

}

Upvotes: 1

Rajpoot Vikram
Rajpoot Vikram

Reputation: 11

use this method this vl definitely work 100%.

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
{
   GalleryCell *cell1 = (GalleryCell*)[_resumeCollectionView cellForItemAtIndexPath:indexPath];
    cell1= nil;

}

Upvotes: 1

eduludi
eduludi

Reputation: 1693

On GalleryCell.m you need to add prepareForReuse method and there nullify the _image variable (assuming that you have a image @property in the cell):

- (void)prepareForReuse {
    [super prepareForReuse];
    [self setHighlighted:NO];

    _image = nil;
}

Upvotes: 7

Rameez Hussain
Rameez Hussain

Reputation: 6764

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! :)

Upvotes: 1

danh
danh

Reputation: 62676

Since the cells are being reused, you must set their contents unconditionally in cellForIndexPath:. Your code as it is sets the images eventually, but it leaves the chance for the images to be temporarily left as old values -- values set before they were reused.

A quick solution is (probably) to provide that setImageWithURL call with a placeholder image. I don't know the SDImage library, but it's a good guess that it immediately sets an imageView's image to the placeholder image if one is provided.

If you don't have or don't want a placeholder image, you can implement prepareForReuse in your UICollectionViewCell subclass and clear the imageView there.

Upvotes: 0

user2872882
user2872882

Reputation: 81

The following is stated in the "UICollectionView Class Reference":" If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithFrame: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead."

Do you have a prepareForReuse method for your GalleryCell cell class?

you cell class should be a subclass of UICollectionReusableView Class. And check it.

Upvotes: 5

Related Questions