Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

swift - Invalid update: invalid number of items in section 0.

Recently I got the following error:

Fatal Exception: NSInternalInconsistencyException Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (13) must be equal to the number of items contained in that section before the update (12), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

The error occurs in the following code in my tvOS client:

 let removedIndexPaths = removedIndexes.map({ IndexPath(row: $0, section: 0) })
 let addedIndexPaths = addedIndexes.map({ IndexPath(row: $0, section: 0) })
 let updatedIndexPaths = updatedIndexes.map({ IndexPath(row: $0, section: 0) })

  self.collectionView?.performBatchUpdates({
      self.collectionView?.deleteItems(at: removedIndexPaths)
      self.collectionView?.insertItems(at: addedIndexPaths)
      }, completion: { _ in
          guard let collectionView = self.collectionView else {
              return
          }

          for indexPath in updatedIndexPaths {
              if let myCell = collectionView.cellForItem(at: indexPath) as? MyCollectionViewCell {
                  let item = self.dataManager.items[indexPath.row]
                  myCell.updateUI(item)
               }
          }

          let collectionViewLayout = self.collectionViewLayoutForNumberOfItems(self.dataManager.items.count)
          if collectionViewLayout.itemSize != self.collectionFlowLayout.itemSize {
                collectionView.setCollectionViewLayout(collectionViewLayout, animated: false)
          }
  })

I am only using one section in my collection view:

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

I have checked out couple of posts on the same topic, but they have not solved my problem, my guess is that the problem is in the following two lines, but I am not sure:

 self.collectionView?.deleteItems(at: removedIndexPaths)
 self.collectionView?.insertItems(at: addedIndexPaths)

Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The call to insertItems(at:) and deleteItems(at:) must be accompanied with change in the datasource as well.

So, before calling these APIs, you would want to change your datasource, i.e. add objects into it before calling insertItems and remove objects from it before calling deleteItems


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...