9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UICollectionViewでSDWebImageを表示するときにindexPathがzombieになる

Posted at

概要

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のカスタムクラスにタップ時ジェスチャーを実装

参考

UICollectionView mysterious crash

9
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?