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];
Share:
27,149

Related videos on Youtube

Luda
Author by

Luda

Updated on July 09, 2022

Comments

  • Luda
    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
      CRDave about 10 years
      You are getting all cell null or you get some cell and after that u get null?
    • nteissler
      nteissler over 4 years
      Though the answers here work, if you can, it's better to call [cell finishExam] directly on your data model instead of proxing through the collectionView. See my comment on Paul S.'s answer. In most cases, you should be able to get cell layout information from the collectionViewLayout property, and model specific functionality like - finishExam should be available on your model, or configured when the cell is dequeued.
  • zyzof
    zyzof over 9 years
    This 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
    Richard McCluskey about 8 years
    Calling [collectionView layoutIfNeeded] worked for me to acquire the cell after calling [collectionView reloadData]. Thanks @zyzof
  • Happiehappie
    Happiehappie almost 8 years
    Thanks 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
    Yuchen about 7 years
    Hey @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 returning nil all the time!
  • Yuchen
    Yuchen about 7 years
    Hey @zyzof, you should totally post your suggestion as a separate answer!! That works great for me!
  • David
    David over 6 years
    If you 'drag' the cell outside the collection view's frame, it will not be visible.
  • nteissler
    nteissler over 4 years
    Calling layoutIfNeeded worked for me. However, I didn't like the explicit need to call that. In my specific use case, I was subclassing UICollectionViewFlowLayout and calling collectionView.cellForItemAt in my layoutAttributesForItem(at indexPath:) implementation. I was able to get all the information for cell layout I needed from super.layoutAttributesForItem(at indexPath: IndexPath).
  • jedwidz
    jedwidz over 3 years
    On iOS 10 and above nil from UICollectionView's cellForItem(at indexPath) can be caused by prefetching, see stackoverflow.com/questions/40322995/…