UICollectionView assertion error on stale data

I run in the same problem. Code runs under 6.1 and crashes under 7.0 I solved the issue the following way:

In the function

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView

I call

[myCollectionView.collectionViewLayout invalidateLayout];

That´s all.


With iOS 10 and 11 this helps:

collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()

Invalidate layout should be AFTER reload data.


Both Dominic Sander and user1544494 are right and their solutions are good.

Unfortunately, I've noticed that if you set minimumLineSpacingForSectionAtIndex or minimumInteritemSpacingForSectionAtIndex, the look of your collectionView is going to break (sooner or later).

Putting invalidateLayout in viewWillLayoutSubviews answers this question and helps preserving the look of viewCollection.

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [viewCollection.collectionViewLayout invalidateLayout];
}

It's simple. Just like that below sentence.

'UICollectionView recieved layout attributes for a cell with an 
index path that does not exist: <NSIndexPath: 0xb141c60> {length = 2, path = 0 - 2}

It's mean that there is no indexPath(0,2) on dataSouce. But, your UICollectionViewLayout return a UICollectionViewLayoutAttributes for indexPath(0,2).

You should return UICollectionViewLayoutAttributes just only exists on dataSouce.


I think that It was changed from iOS7.