I’ve been getting the following error after trying to implement deletion for a Collection View:
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (3) must be equal to the number of sections contained in the collection view before the update (5), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).’
I currently have 5 sections, and there’s one item in each section. Based on the above message, it seems that nothing is even deleted when I call:
@IBAction func deleteButtonTapped(_ sender: UIBarButtonItem) {
let indexPaths = collectionView!.indexPathsForSelectedItems! as [IndexPath]
favs.deleteItemsAtIndexPaths(indexPaths)
collectionView!.deleteItems(at: indexPaths)
The data source seems to be being updated because it registers and removes the items I selected to remove. This is my code to update the data source:
func deleteItemsAtIndexPaths(_ indexPaths: [IndexPath]) {
var indexes: [Int] = []
for indexPath in indexPaths {
indexes.append((indexPath as NSIndexPath).section)
var newArtists: [FavoriteArtist] = []
for (index, artist) in artistsArray.enumerated() {
if !indexes.contains(index) {
newArtists.append(artist)
artistsArray = newArtists
Any advice here would be appreciated!
Hi @bhuff1
Try to wrap your updates in the ‘performBatchUpdates’:
self.collectionView.performBatchUpdates({
favs.deleteItemsAtIndexPaths(indexPaths)
collectionView.deleteItems(at: indexPaths)
}) { (finished) in
self.collectionView.reloadIData()
Nikita
Nikita,
I tried your suggested code like this:
@IBAction func deleteButtonTapped(_ sender: UIBarButtonItem) {
let indexPaths = collectionView!.indexPathsForSelectedItems! as [IndexPath]
self.collectionView!.performBatchUpdates({
favs.deleteItemsAtIndexPaths(indexPaths)
self.collectionView!.deleteItems(at: indexPaths)
}) { (finished) in
self.collectionView!.reloadData()
But the same error occurred:
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (3) must be equal to the number of sections contained in the collection view before the update (5), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).’
Hi @bhuff1! Could you go into a few more details about your collection view? The exception that you are receiving is describing a mismatch in sections and not individual items.
I’m assuming that this is because you are separating your content into sections? If this is the case then the deleteItems(at:)
function is not enough.
If your data source logic doesn’t is excluding sections when there are no items within them then you must also detect this in your delete function and remove these sections using the deleteSections(_:)
function instead of removing each individual item within that section.
This means that your delete logic will need to capture the previous state of your data source, work out what sections (if any) that it needs to completely remove and then work out what individual items it also needs to remove. All of these calls should be called in the performBatchUpdates
function that @nikita_gaydukov mentioned.
If that is all a bit too much then you can just call reloadData()
instead but you won’t have any animations so it’s up to you. Good luck