Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Crash on collectionView.deleteSections(...) #7

Open
ostatnicky opened this issue Sep 29, 2015 · 4 comments
Open

Crash on collectionView.deleteSections(...) #7

ostatnicky opened this issue Sep 29, 2015 · 4 comments

Comments

@ostatnicky
Copy link

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
Copy link
Author

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

@ostatnicky
Copy link
Author

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
Copy link
Author

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
...

@jkereako
Copy link

jkereako commented Aug 5, 2016

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

override func invalidateLayout() {
  super.invalidateLayout()

  itemAttributes = nil
  itemsSize = nil
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants