Search code examples
objective-cios7uicollectionviewuigesturerecognizeruicollectionviewcell

UICollectionView crashes randomly because of highlighting issue


I have a UICollectionView on iOS7 which crashes randomly when intense scrolling. I enabled zombies and found that it gives me an error saying:

*** -[NSIndexPath section]: message sent to deallocated instance 0x17dbc970

I believe this is due to an Apple error described here. Apparently, the app crashes when someone highlights a cell while scrolling fast, and then the OS tries to unhighlight it when it moves off screen, when it ceases to exist. The proposed solution is to disable the userInteractionEnabled property of the cell and then handle the selection using UIGestureRecogniser.

Has anyone else faced this same issue? Also, I tried unsetting the userInteractionEnabled property and using a gesture recogniser, but this doesn't seem to work. Any idea how I can fix this?

EDIT: Code added on request

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

NSString *CellIdentifier = @"Gallery_Cell";

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

if (indexPath.row < self.collectionData.count) {

    CellDetails *dets = [self.collectionData objectAtIndex:indexPath.row];

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

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

    if ([[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:[self cacheKeyForURL:mainImageURL]] == nil) {

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

    }else{

          [cell.image setImage:[[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:[self cacheKeyForURL:mainImageURL]]];

    }
}

return cell;
}

EDIT: more code..

I defined the GalleryCell for reuse as follows:

[self.flowCollection registerNib:[UINib nibWithNibName:@"Thumbs_Cell" bundle:nil] forCellWithReuseIdentifier:@"Gallery_Cell"];

The GalleryCell class implementation is:

GalleryCell.h

@interface GalleryCell : UICollectionViewCell

@property (nonatomic, retain) IBOutlet UIImageView *image;

@end

GalleryCell.m

@implementation GalleryCell
@synthesize image;

-(void) setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
}

-(void)prepareForReuse {
    [super prepareForReuse];
    [self.image cancelCurrentImageLoad]; // SDWebImage method to cancel ongoing image load
}

Solution

  • OK. I seem to have solved it. In case anyone faces this problem, here is the fix:

    I implemented the following method in my UICollectionViewDelegate:

    -(BOOL) collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    
        return NO;
    }
    

    This prevents any cell from highlighting, and hence, avoids the crash when the system tries to unhighlight it when it goes off-screen. But, when you do this it also stops calling the didSelectItemAtIndexPath method. So I had to use a UITapGestureRecogniser method to implement cell selection instead.

    Hope this helps.