Delete items in collection view with custom layout

Afternoon All


I have a collection view with a simple custom layout (each cell is a simple image) and am getting errors when tryig to delete items, i.e. "assertion failure in -[UICollectionViewData validateLayoutInRect]" and then also "UICollectionView received layout attributes for an index ath that does not exist". The second error crashes the process.


I've created a very simple test, without custom layout, as follows:

// This works

items.remove(at: 0)

cv.deleteItems(at: [IndexPath(row: 0, section: 0)])

cv.reloadData()


This works fine. I've tried at least a dozen solutions I found online but nothing works. I've tried batch updates, setting delegates to nil and then resetting after the updates, invalidating the layout etc.


The error is almost always on the line "cv.deleteItems(at: [IndexPath(row: 0, section: 0)])", sometimes in the app delegate.


Does anyone have a simple checklist of what I'd need to do to get this working with custom layout?


Many thanks...

Excellent, thanks again.


I must have messed up somewhere. I modified my code to clear the items from the array and then do a reload as opposed to "deleteItems(at", thought it'd be simple if the source was modified first. Fingers crossed it'll just be a simple mistake.


Have a good weekend and thanks again!

Delete items in collection view with custom layout
 
 
Q