Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong image load in uicollectionviewcell when scrollling

I am using a UICollectionview to show a lot of Custom cells (250 more or less).

Those cells have a main Image and some text. As the images have to be downloaded from the Internet I am using the external library AsyncImageView to do the lazy load stuff.

But the problem is that the reusable property of the cells are making me crazy.

When I scroll the images appear in the wrong cells. How can I add a tag or something to the images apart from the indexpath to avoid the problem?

Maybe AsyncImageView has a solution to the problem which I ignore ...

Or another alternative would be a better choice?

Any clue?

Thanks in advance

Edit: A simplified version of my code

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

static NSString *identifier = @"Cell";

CollectionComercioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


if (cell == nil){
}
else{
    [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget: cell.myImage];
}

cell.myImage.imageURL = nil;
cell.myImage.image = nil;

cell.myImage.hidden = TRUE;

cell.myImage.imageURL = [[myArray objectAtIndex:indexPath.row] getLogoUrl];

cell.myText.text = [[myArray objectAtIndex:indexPath.row] getName];

cell.myImage.hidden = FALSE;

return cell;

}

CustomCell.m

- (void)prepareForReuse
{
    [super prepareForReuse];    
    self.myImage.image = nil;  
}
like image 918
Andoxko Avatar asked Jan 15 '14 08:01

Andoxko


1 Answers

Make sure you set the image to nil in your cellForRowAtIndexPath: method. This way it will stay at least empty until the new image is loaded.

As you mentioned in your comments to the question that this is works if you scroll slow and not when you scroll fast you could maybe override - (void)prepareForReuse on your custom cell. But be aware that Apple warns you not to use it for content changes:

For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

like image 83
Pfitz Avatar answered Oct 14 '22 05:10

Pfitz