Incorrect image loading in uicollectionviewcell when scrolling

I use UICollectionview to show many custom cells (more than 250 or less).

These cells have a main image and some text. Since the images must be downloaded from the Internet, I use the AsyncImageView external library to work with lazy downloads.

But the problem is that the reusable cell property makes me crazy.

Incorrect cells appear when scrolling images. How to add a tag or something to images other than the index path to avoid the problem?

Perhaps AsyncImageView has a solution to a problem that I ignore ...

Or is another option a better choice?

Any clue?

Thank you in advance

Edit: 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; } 
+6
source share
1 answer

Make sure the image is set to zero in the cellForRowAtIndexPath: method. Thus, it will remain at least empty until a new image is loaded.

As you mentioned in your comments on the question that this works if you scroll slowly, and not when you scroll quickly, you can override - (void)prepareForReuse in your custom cell. But keep in mind that Apple warns you not to use it for content changes:

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

+5
source

All Articles