CustomCollectionViewLayout icon indicating copy to clipboard operation
CustomCollectionViewLayout copied to clipboard

Crash on collectionView.deleteSections(...)

Open ostatnicky opened this issue 9 years ago • 4 comments

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?

ostatnicky avatar Sep 29 '15 13:09 ostatnicky

I simulated on my branch: https://github.com/ostatnicky/CustomCollectionViewLayout

ostatnicky avatar Sep 29 '15 14:09 ostatnicky

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.

ostatnicky avatar Sep 29 '15 14:09 ostatnicky

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 avatar Sep 30 '15 11:09 ostatnicky

@ostatnicky UICollectionViewLayout has a delegate method just for what you're doing.

override func invalidateLayout() {
  super.invalidateLayout()

  itemAttributes = nil
  itemsSize = nil
}

jkereako avatar Aug 05 '16 14:08 jkereako