由于突出显示问题,UICollectionView随机崩溃

Ram*_*ain 4 objective-c uigesturerecognizer uicollectionview uicollectionviewcell ios7

我有一个UICollectionViewiOS7强烈滚动时随机崩溃的东西.我启用了僵尸,发现它给了我一个错误说:

*** -[NSIndexPath section]: message sent to deallocated instance 0x17dbc970
Run Code Online (Sandbox Code Playgroud)

我相信这是由于此处描述的Apple错误.显然,当有人在快速滚动时突出显示一个单元格时,应用程序会崩溃,然后当操作系统离开屏幕时,操作系统会在它不再存在时尝试取消它.建议的解决方案是禁用userInteractionEnabled单元格的属性,然后使用处理选择UIGestureRecogniser.

还有其他人遇到同样的问题吗?此外,我尝试取消设置userInteractionEnabled属性并使用手势识别器,但这似乎不起作用.知道如何解决这个问题吗?

编辑:代码根据要求添加

-(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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:更多代码..

我将GalleryCell重用定义如下:

[self.flowCollection registerNib:[UINib nibWithNibName:@"Thumbs_Cell" bundle:nil] forCellWithReuseIdentifier:@"Gallery_Cell"];
Run Code Online (Sandbox Code Playgroud)

GalleryCell类的实现是:

GalleryCell.h

@interface GalleryCell : UICollectionViewCell

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

@end
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

Ram*_*ain 9

好.我好像已经解决了.如果有人遇到这个问题,这里是修复:

我在我的实现了以下方法UICollectionViewDelegate:

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

    return NO;
}
Run Code Online (Sandbox Code Playgroud)

这可以防止任何单元格突出显示,因此,当系统在屏幕外时尝试取消突出显示时,可以避免崩溃.但是,当你这样做时,它也停止调用该didSelectItemAtIndexPath方法.所以我不得不使用一种UITapGestureRecogniser方法来实现细胞选择.

希望这可以帮助.