I have a UICollectionView that uses a custom subclass of UICollectionViewFlowLayout to make the section headers stick to the top of the screen while scrolling just like UITableV
I'm now getting a crash that looks similar, i.e. where the layout is out of sync with the data source. This one I can reproduce and I believe I've found the cause. Since it is similar, I'm wondering if it is the same cause as my original crash/question. The error is:
UICollectionView recieved [sic] layout attributes for a cell with an index path that does not exist
My UICollectionViewFlowLayout
subclass implements shouldInvalidateLayoutForBoundsChange:
(so that I can position section headers at the top of the viewable bounds as the user scrolls). If a bounds change occurs and that method returns YES
, the system requests a light-weight layout invalidation, i.e. all the properties on the invalidation context that is passed to the layout are NO
: invalidateEverything
, invalidateDataSourceCounts
, invalidateFlowLayoutAttributes
and invalidateFlowLayoutDelegateMetrics
. If reloadData
is then called afterwards (presumably before the layout has been reprocessed) then the system will not automatically call invalidateLayout
, i.e. it will not trigger a heavy-weight invalidation context where all those properties are YES
. So when that happens the layout apparently does not update its internal cache of section/item counts and such, which the leads to the layout being out of sync with the data source and a crash.
So for the particular situation I just ran into, I was able to work around it by simply calling invalidateLayout
myself after calling reloadData
. I'm not sure if this is the same situation as my original question/crash but it sure sounds like it.