ios UICollectionView cellForItemAtIndexPath 为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22861804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView cellForItemAtIndexPath is nil
提问by Luda
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.
我有一个更新现有 UICollectionView 的函数。UICollectionView 已创建,我可以看到它,但是当我想访问它的单元格来更新它时,它们为零。
-(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;
}
Log:
日志:
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
回答by Paul.s
From the UICollectionViewdocs (emphasis my own)
来自UICollectionView文档(强调我自己的)
Return Value
The cell object at the corresponding index path or nil if the cell is not visibleor indexPath is out of range.
返回值
对应索引路径的单元格对象,如果单元格不可见或 indexPath 超出范围,则为nil。
You should update your underlying model, which provides the data to the views.
您应该更新您的底层模型,它为视图提供数据。
回答by zyzof
Try this:
尝试这个:
[collectionView reloadData];
[collectionView layoutIfNeeded];