iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist
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 VersioncollectionView.reloadData()collectionView.collectionViewLayout.invalidateLayout()
When you have custom layout, remember to clear cache (UICollectionViewLayoutAttributes) while overriding prepare func
override func prepare() { super.prepare() cache.removeAll() }
I also faced this issue with 2 collectionViews in the same view because I added the same UICollectionViewFlowLayout in both collections :
let layout = UICollectionViewFlowLayout()collectionView1.collectionViewLayout = layoutcollectionView2.collectionViewLayout = layout
Of course it crashes on reload data if collectionView1 Data change. If this could help someone.
So you should do this instead
let layout1 = UICollectionViewFlowLayout()collectionView1.collectionViewLayout = layout1let layout2 = UICollectionViewFlowLayout()collectionView2.collectionViewLayout = layout2