6

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;  
}
Andoxko
  • 941
  • 2
  • 10
  • 17
  • have you tried to set the image of the cell to nil before adding a new image to it? This should now show a image in the cell – Pfitz Jan 15 '14 at 08:29
  • Yo were right @Pfitz now the images stay in the correct `cell` if a scroll slowly. But when I scroll fast a wrong image is shown for a second or less (the image that was on that `indexpath` before I scroll) Can be this avoided ? ThankYou – Andoxko Jan 15 '14 at 08:49

1 Answers1

5

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.

Pfitz
  • 7,226
  • 4
  • 36
  • 51
  • I have edited the question with the changes you have tell me but unfortunately it does not work for me. The images still appear in wrong places for a second or less... – Andoxko Jan 15 '14 at 09:10
  • Thanks for highlighting this. I've been using prepareForReuse in this way, good to know it's not always advisable. – joel.d Oct 09 '15 at 20:02
  • @joel.d Is this warning for UICollectionViewCell? The class docs for `UICollectionReusableView` don't seem to reference this warning in the description for `prepareForReuse`: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionReusableView_class/ – Crashalot Apr 10 '16 at 09:46
  • @Crashalot Good point, although the docs for UICollectionReusableView do say: "You should not use this method to assign any new data to the view. That is the responsibility of your data source object." My sense is that for both UITableViewCell and UICollectionReusableView, prepareForReuse should only be used to set "attributes not related to content". – joel.d Apr 10 '16 at 22:31