CustomCollectionViewLayout
CustomCollectionViewLayout copied to clipboard
Crash on collectionView.deleteSections(...)
If I add a button that is removing some section, it is crashing after scrolling to last section:
@IBAction func didClick(sender: AnyObject) {
numberOfSections -= 1;
let set = NSIndexSet(index: 2)
self.collectionView.deleteSections(set)
}
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000003116> {length = 2, path = 49 - 0}'
Can you help me how to resolve this issue?
I simulated on my branch: https://github.com/ostatnicky/CustomCollectionViewLayout
I added
func reload() {
itemAttributes = nil
itemsSize = nil
prepareLayout()
}
into CustomCollectionViewLayout
and call it after deleting section, but it's still crashing when I'm on bottom of collection view.
Hey, I've niled at start of prepareLayout
the itemAttributes
and itemsSize
and it works now. It is a little hot fix but it works.
override func prepareLayout() {
itemAttributes = nil
itemsSize = nil
...
@ostatnicky UICollectionViewLayout has a delegate method just for what you're doing.
override func invalidateLayout() {
super.invalidateLayout()
itemAttributes = nil
itemsSize = nil
}