I'm trying to delete a cell from my UICollectionView. I'm using a custom layout, and the deletion happens in response to a long press gesture recognizer that I've added to the collection view. From what I've read, I need to update the data source first, then delete the cells, which is what I do. Everything goes smoothly until self.collectionView?.deleteItemsAtIndexPaths([indexPath])
. I get the error:
*** 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 = 0 - 0}'
I've read other similar SO questions, but can't seem to get it working.
let book = self.booksForCurrentAuthor[indexPath.row]
self.booksForCurrentAuthor.removeAtIndex(indexPath.row)
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
//Delete book from database
Edit
I don't think this is relevant, but in my custom layout object, I create a cache of UICollectionViewLayoutAttributes
from within the prepareLayout()
method. The cache is a dictionary, with separate arrays for attributes of Headers, Cells, Footers, and Decoration views.