当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];