0

After removing the last item in a section, the goal is to remove the entire section, including its header, from the UICollectionView. Unfortunately, the section header persists even though there is no section in the underlying data model.

Refreshing the view -- by popping it from the navigation stack and navigating back to the view -- correctly shows the UICollectionView with the section header removed.

In the test case, there is only one section so the UICollectionView should become blank after removing the final item.

Suggestions?

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    print("# sections: \(USER.getSections().count)")
    return USER.getSections().count
}

    // Delete item
    let indexPath = view.indexPathsForSelectedItems()![0]
    let section = USER.getSections()[indexPath.section]
    USER.removeItemAt(section, index: indexPath.row)
    view.deleteItemsAtIndexPaths([indexPath])

    // Update one section or entire view
    if (section.getNumItems() > 0) {
        view.reloadSections(NSIndexSet(index: indexPath.section))
    } else {
        view.deleteSections(NSIndexSet(index: indexPath.section))
        view.reloadSections(NSIndexSet(index: indexPath.section))
        view.reloadData()
    }
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

1

When you want to remove a section from a UICollectionView you must: 1. update your model, 2. call deleteSections(sections: NSIndexSet) on your collection view 3. ensure that numberOfSectionsInCollectionView returns the appropriate number of sections for your updated model.

beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • Thanks for your help! Is there a potential issue on removing the last section in a UICollectionView? Because now this error appears even though we're calling `deleteSections(sections: NSIndexSet)` and the data model shows the right number of sections (confirmed by printing value in `numberOfSectionsInCollectionView`)? – Crashalot Jan 21 '16 at 03:05
  • The error is `'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (0) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'` – Crashalot Jan 21 '16 at 03:06
  • Hmmm noticed it says, `0 deleted` yet we're calling `deleteSections(sections: NSIndexSet)`, as you can see in the updated code. – Crashalot Jan 21 '16 at 03:08
  • Sorry didn't update the post yet. Yes, we are calling `view.deleteSections(Set([indexPath.section]))` which the post now shows. – Crashalot Jan 21 '16 at 03:24
  • Call reloadSections after you do the deleteSections. That is why it's throwing the error. You've updated your model but have yet to delete the section. – beyowulf Jan 21 '16 at 03:28
  • Doesn't `reloadData` reload the whole collection, including the section? – Crashalot Jan 21 '16 at 07:41
  • Or are you suggesting to include both `reloadData` and `view.deleteSections(Set([indexPath.section]))` when `section.getNumItems() == 0`? – Crashalot Jan 21 '16 at 08:07
  • The following error still occurs, even with the code you suggested (updated post to show new code and `numberOfSectionsInCollectionView`. The error suggests the deletion isn't occurring, even though the code is calling `deleteSections(sections: NSIndexSet)`. Error: `'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (0) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'` – Crashalot Jan 21 '16 at 08:14
  • Thanks for all your help! – Crashalot Jan 21 '16 at 10:03
  • Also do you happen to know the answer to this? http://stackoverflow.com/questions/34912050/avoiding-blurriness-at-start-end-of-video-even-after-using-setpreferredvideos?noredirect=1#comment57575685_34912050 – Crashalot Jan 21 '16 at 10:57