4

我在iOS7上有UICollectionView,它在密集滾動時隨機崩潰。我啓用殭屍和發現,它給了我一個錯誤說:UICollectionView由於突出顯示的問題而隨機崩潰

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

我相信這是由於蘋果的錯誤描述here。顯然,當有人在快速滾動的同時突出顯示某個單元格時,該應用程序會崩潰,然後當操作系統從屏幕移出並停止存在時,該操作系統會嘗試忽略它。建議的解決方案是禁用單元格的userInteractionEnabled屬性,然後使用UIGestureRecogniser處理選擇。

有沒有其他人面臨同樣的問題?此外,我試圖取消設置userInteractionEnabled屬性並使用手勢識別器,但這似乎不起作用。任何想法如何我可以解決這個問題?

EDIT:代碼加在請求

-(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:多個代碼..

我所定義的GalleryCell重用如下:

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

的GalleryCell類實施是:

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 
} 
+0

你一個數據加載到從核心數據細胞? 'cellForRowAtIndexPath:'正在工作時可能會發生這種崩潰。 –

+0

我正在使用SDWebImage將圖像加載到Internet的單元格中。 –

+0

所以你應該小心使用它,因爲每次單元變得可見時,'UICollectionView'調用'cellForRowAtIndexPath:'。 –

回答

9

確定。我似乎已經解決了它。如果任何人都面臨這個問題,這裏是修復:

我實現下面的方法在我UICollectionViewDelegate

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

    return NO; 
} 

這可以防止突出的任何細胞,因此,當系統試圖避免碰撞當它離開屏幕時,不要高興它。但是,當你這樣做時,它也會停止調用didSelectItemAtIndexPath方法。所以我不得不使用UITapGestureRecogniser方法來實現單元格選擇。

希望這會有所幫助。

+0

我似乎有同樣的問題,但只有很少(並且不能在我的開發環境中重現)。您是否考慮檢查indexPath是否在'shouldHighlightItem ...'中可見,而不是使用輕敲識別器。例如。 'return [[collectionView indexPathsForVisibleItems] containsObject:indexPath];'。我打算放棄它,並希望它清除我的客戶看到的問題。 – Loz

+0

嗯。我沒有真正考慮這些方面,所以我沒有這樣檢查。但問題停止發生,所以我把它留在這個解決方案。但你的方法祝你好運! –

+0

需要注意的是,我們在崩潰分析中看到了同樣的崩潰,並且在超過3000次的事件中設備運行iOS 7. iOS 8出現了0次。 – Chris

1

我會建議返回以下:

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

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