My code works fine on iOS 11, but it crashes occasionally on iOS 12. The error I get is:
Assertion failure in -[UICollectionViewData validateLayoutInRect:]. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 1 - 0}
I have a collection view showing several sections. I reload one of the sections when I get new data from the server. The code I use to reload the section is:
let attributes = calculateAttributesFrom(items)
layout.firstSectionAttributes = attributes
collectionView.reloadSections([kFirstSectionIndex])
The number of items in a section is:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if indexPath.section == kFirstSectionIndex {
return layout.firstSectionAttributes.count
}
...
}
I use layout.firstSectionAttributes.count
as the number of items in the sections. This means each attribute must have a corresponding cell. I guess this is related to layout.invalidateLayout()
. I tried to add it before and after the reload, but it did not help.
Does anyone have any idea?
Edit
It does not crash anymore after I replace reloadSections
with reloadData
. But I only want to reload one section instead of all of them.
invalidationContext
wherelet invalidationContext = super.invalidationContext(forBoundsChange: newBounds)
inoverride func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext
. And take care ofoverride func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
andoverride func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool)
... – user5924595prepare()
method in my customUICollectionViewFlowLayout
class so I don't have cache. I wanted to remove all the existing data and recreate based on the new data provided(reduced number of rows) – user121095