由于突出显示问题,UICollectionView随机崩溃 [英] UICollectionView crashes randomly because of highlighting issue
问题描述
我在 iOS7
上有 UICollectionView
,在强烈滚动时会随机崩溃。我启用了僵尸并发现它给我一个错误说:
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
我认为这是由于描述的一个Apple错误这里。显然,当有人在快速滚动时突出显示一个单元格时,应用程序会崩溃,然后当操作系统离开屏幕时,操作系统会在它不再存在时尝试取消它。建议的解决方案是禁用单元格的 userInteractionEnabled
属性,然后使用 UIGestureRecogniser
处理选择。
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
.
还有其他人遇到同样的问题吗?此外,我尝试取消设置 userInteractionEnabled
属性并使用手势识别器,但这似乎不起作用。我知道如何解决这个问题吗?
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?
编辑:根据要求添加代码
-(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;
}
编辑:更多代码..
我将 GalleryCell
定义为重用,如下所示:
I defined the GalleryCell
for reuse as follows:
[self.flowCollection registerNib:[UINib nibWithNibName:@"Thumbs_Cell" bundle:nil] forCellWithReuseIdentifier:@"Gallery_Cell"];
GalleryCell类的实现是:
The GalleryCell class implementation is:
GalleryCell.h
GalleryCell.h
@interface GalleryCell : UICollectionViewCell
@property (nonatomic, retain) IBOutlet UIImageView *image;
@end
GalleryCell.m
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
}
推荐答案
行。我好像已经解决了。如果有人遇到这个问题,这里有修复:
OK. I seem to have solved it. In case anyone faces this problem, here is the fix:
我在 UICollectionViewDelegate
中实现了以下方法:
I implemented the following method in my UICollectionViewDelegate
:
-(BOOL) collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
这可以防止任何单元格突出显示,从而避免系统崩溃当它离开屏幕时试图取消它。但是,当你这样做时,它也停止调用 didSelectItemAtIndexPath
方法。所以我不得不使用 UITapGestureRecogniser
方法来实现单元格选择。
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.
希望这会有所帮助。
这篇关于由于突出显示问题,UICollectionView随机崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!