概要
SDWebImageで画像ロードを行ってCollectionViewで表示。タップしたらpushViewControllerする。というありがちな実装を行った時にindexPathがzombieになる現象が発生。その時の対応メモ。
ロードする画像が縦横1000px以上のiPad用大きめサイズのものを読み込む際に現象は頻発。xib使用。
修正前
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_cellIdentifier forIndexPath:indexPath];
// item取得
HogeClass item = self.items[indexPath.row];
// item.urlを使用して画像読み込み
[self.thumbnail sd_setImageWithURL:[NSURL URLWithString:item.url] placeholderImage:nil options:0 progress:nil needIndicatorColor:NO completed:nil];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// タップしたらpushViewContollerする処理
}
※cellのregistNib等は行っているものとする
実機(iPad)を起動させて大量に画像を読み込んでスクロールをつづけると
zombieが発生する。
対応
その1 shouldHighlightItemAtIndexPathを追加
- (BOOL) collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
その2
- didSelectItemAtIndexPathの廃止
- CollectionViewCellのカスタムクラスにタップ時ジェスチャーを実装