UICollectionView crashes randomly because of highlighting issue

扶醉桌前 提交于 2019-12-12 08:55:10

问题


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
}

回答1:


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.




回答2:


I would suggest returning the following:

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

        return !collectionView.dragging && !collectionView.tracking;
}


来源:https://stackoverflow.com/questions/20453291/uicollectionview-crashes-randomly-because-of-highlighting-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!