CollectionViewCell not visible, cellForItemAtIndexPath return nil

当collectionViewCell未显示时,调用cellForItemAtIndexPath返回nil

解决方法是:先让cell可见,刷新后再获取:

// 记录原indexPath
NSIndexPath *orginIndexPath = indexPath;

// 滚动collectionView到需要获取的那个indexPath对应的cell
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];

UICollectionViewCell *endView = [collectionView cellForItemAtIndexPath:indexPath];
// 当获取不到时,刷新下再获取
 if (!endView) {
      [collectionView layoutIfNeeded];
      endView = [collectionView cellForItemAtIndexPath:indexPath];
                                                    }
                                                    
if(!endView) {
    [collectionView reloadData];
     [collectionView layoutIfNeeded];
     endView = [collectionView cellForItemAtIndexPath:indexPath];
                                                    }
                                                    
// 获取完成后再回到原位置
[collectionView scrollToItemAtIndexPath:orginIndexPath atScrollPosition:0 animated:NO];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容