添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
慈祥的佛珠  ·  Collection View ...·  19 小时前    · 
英姿勃勃的针织衫  ·  How to Fix the ...·  3 天前    · 
怕老婆的荒野  ·  IF formula to change ...·  1 周前    · 
傻傻的伤痕  ·  How to: Apply Rich ...·  1 周前    · 
粗眉毛的作业本  ·  Applying a data ...·  1 周前    · 
小眼睛的脆皮肠  ·  Checkpointing under ...·  4 月前    · 
爱吹牛的人字拖  ·  印象空间 - ...·  6 月前    · 

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 :slightly_smiling_face: