brightec / CustomCollectionViewLayout

Custom layout for a collection view using horizontal and vertical scrolling with sticky rows and columns
MIT License
519 stars 123 forks source link

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

Open ostatnicky opened 8 years ago

ostatnicky commented 8 years ago

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 commented 8 years ago

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

ostatnicky commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

override func invalidateLayout() {
  super.invalidateLayout()

  itemAttributes = nil
  itemsSize = nil
}