Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 600
Rameez Hussain Avatar asked Dec 25 '22 16:12

Rameez Hussain


2 Answers

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.

like image 156
Rameez Hussain Avatar answered Dec 28 '22 08:12

Rameez Hussain


I would suggest returning the following:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

        return !collectionView.dragging && !collectionView.tracking;
}
like image 21
Dmitrij Mazurenko Avatar answered Dec 28 '22 10:12

Dmitrij Mazurenko