Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist

Running my app in a device with iOS 10 I get this error:

UICollectionView received layout attributes for a cell with an index path that does not exist

In iOS 8 and 9 works fine. I have been researching and I have found that is something related to invalidate the collection view layout. I tried to implement that solution with no success, so I would like to ask for direct help. This is my hierarchy view:

->Table view      ->Each cell of table is a custom collection view [GitHub Repo][1]         ->Each item of collection view has another collection view 

What I have tried is to insert

    [self.collectionView.collectionViewLayout invalidateLayout]; 

In the

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 

of both collection views.

Also I have tried to invalidate layout before doing a reload data, does not work...

Could anyone give me some directions to take?

like image 361
cmacera Avatar asked Oct 05 '16 06:10

cmacera


People also ask

What is UICollectionView flow layout?

Overview. A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.

How does UICollectionView work?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.


2 Answers

This happened to me when number of cells in collectionView changed. Turns out I was missing invalidateLayout after calling reloadData. After adding it, I haven't experienced any more crashes. Apple has made some modifications to collectionViews in iOS10. I guess that's the reason why we are not experiencing same problem on older versions.

Here's my final code:

[self.collectionView reloadData]; [self.collectionView.collectionViewLayout invalidateLayout];  //Swift 4.2 Version collectionView.reloadData() collectionView.collectionViewLayout.invalidateLayout() 
like image 115
Katrin Annuk Avatar answered Sep 18 '22 17:09

Katrin Annuk


When you have custom layout, remember to clear cache (UICollectionViewLayoutAttributes) while overriding prepare func

    override func prepare() {        super.prepare()        cache.removeAll()     } 
like image 20
ArturC Avatar answered Sep 19 '22 17:09

ArturC