Search code examples
iosobjective-cuicollectionviewuicollectionviewlayout

UICollectionView with custom UICollectionViewLayout validateLayoutInRect accertion


I'm creating app with collection view with custom collection view layout. All works fine until one cell removed. After [collectionView reloadData] all seems good. But after collection view scrolls application crashes with assertion.

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: <NSIndexPath: 0xc000000000060016> {length = 2, path = 0 - 12}

I already dug into many answers on stackOverflow, trying many things but still can't get rid of this problem.

Small example project with my custom layout and this problem I uploaded here:

https://github.com/fizzy871/CollectionViewCustomLayoutBugFix

There is collection view and "remove cell" button which removes last cell and produce that crash.

Please, who knows why it happens or have some ideas, help me with this problem.

enter image description here


Solution

  • I find out where is problem. I use lazy loading for dictionary with attributes and after layout was invalidated new attributes was added to dictionary. And earlier elements was here too. Now dictionary removed in - prepareLayout and all works good.

    Thank you, Tristan Burnside. You lead me to right way.