UICollectionView cellForItemAtIndexPath is nil
27,149
Solution 1
From the UICollectionView docs (emphasis my own)
Return Value
The cell object at the corresponding index path or nil if the cell is not visible or indexPath is out of range.
You should update your underlying model, which provides the data to the views.
Solution 2
Try this:
[collectionView reloadData];
[collectionView layoutIfNeeded];
Related videos on Youtube
Author by
Luda
Updated on July 09, 2022Comments
-
Luda almost 2 years
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; }
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
-
CRDave about 10 yearsYou are getting all cell null or you get some cell and after that u get null?
-
nteissler over 4 yearsThough the answers here work, if you can, it's better to call
[cell finishExam]
directly on your data model instead of proxing through thecollectionView
. See my comment on Paul S.'s answer. In most cases, you should be able to get cell layout information from thecollectionViewLayout
property, and model specific functionality like- finishExam
should be available on your model, or configured when the cell is dequeued.
-
-
zyzof over 9 yearsThis worked for me, but in addition to reloading the data with [view reloadData], I also had to [view layoutIfNeeded] as per stackoverflow.com/a/21480786/1388195
-
Richard McCluskey about 8 yearsCalling [collectionView layoutIfNeeded] worked for me to acquire the cell after calling [collectionView reloadData]. Thanks @zyzof
-
Happiehappie almost 8 yearsThanks all for the layoutIfNeeded!!! :D Just wondering, does anyone know why though? Cause in my case the cell has been visible the entire time.
-
Yuchen about 7 yearsHey @RichardMcCluskey, do you know why we have to call this? I notice that all the UICollectionViews are rendered properly but indeed, without this line of call
cellForItemAtIndexPath
is returningnil
all the time! -
Yuchen about 7 yearsHey @zyzof, you should totally post your suggestion as a separate answer!! That works great for me!
-
David over 6 yearsIf you 'drag' the cell outside the collection view's frame, it will not be visible.
-
nteissler over 4 yearsCalling
layoutIfNeeded
worked for me. However, I didn't like the explicit need to call that. In my specific use case, I was subclassingUICollectionViewFlowLayout
and callingcollectionView.cellForItemAt
in mylayoutAttributesForItem(at indexPath:)
implementation. I was able to get all the information for cell layout I needed fromsuper.layoutAttributesForItem(at indexPath: IndexPath)
. -
jedwidz over 3 yearsOn iOS 10 and above
nil
fromUICollectionView
'scellForItem(at indexPath)
can be caused by prefetching, see stackoverflow.com/questions/40322995/…