本文介绍了UICollectionView cellForItemAtIndexPath 为 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个更新现有 UICollectionView 的函数.UICollectionView 已创建,我可以看到它,但是当我想访问它的单元格来更新它时,它们是 nil.
I have a function that updates existing UICollectionView.The UICollectionView is created, I can see it, but when I want to access its cells to update it, they are nil.
-(void)finishExam{
for (int i = 0; i < [self.questionsOverviewCollection numberOfItemsInSection:0]; i++) {
NSLog(@"self.questionsOverviewCollection - %@",self.questionsOverviewCollection);
NSLog(@"cell - %@",[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]);
NSLog(@"overviewCell - %@",(OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]);
NSLog(@"numOfCells - %d", [self.questionsOverviewCollection numberOfItemsInSection:0]);
OverviewCell *cell = (OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[cell finishExam];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OverviewCell *cell = (OverviewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell someSetUp];
return cell;
}
日志:
self.questionsOverviewCollection - <UICollectionView: 0xa1abc00; frame = (14 219; 217 441); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0xe0617a0>; layer = <CALayer: 0xe0bbb00>; contentOffset: {0, 0}> collection view layout: <UICollectionViewFlowLayout: 0xe0cc3f0>
cell - (null)
overviewCell - (null)
numOfCells - 30
推荐答案
来自 UICollectionView 文档(强调我自己的)
From the UICollectionView docs (emphasis my own)
返回值
对应索引路径处的单元格对象;如果单元格不可见,则为 nil 或 indexPath 超出范围.
你应该更新你的底层模型,它为视图提供数据.
You should update your underlying model, which provides the data to the views.
这篇关于UICollectionView cellForItemAtIndexPath 为 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!