无效的更新:第0部分中的项目数无效. [英] Invalid update: invalid number of items in section 0.

查看:287
本文介绍了无效的更新:第0部分中的项目数无效.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到以下错误:

致命异常:NSInternalInconsistencyException 无效的更新:第0节中的项目数无效.更新(13)之后必须包含在现有节中的项目数 等于该节之前该部分中包含的项目数 更新(12),加上或减去插入或删除的项目数 从该部分开始(插入0,删除0),然后加上或减去数字 移入或移出该部分的项目(移入的是0,移出的是0).

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).

我的tvOS客户端中的以下代码中发生错误:

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)

请帮助.

推荐答案

insertItems(at :) deleteItems(at:)也必须伴随数据源的变化.

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

因此,在调用这些API之前,您需要更改数据源,即在调用insertItems之前将对象添加到其中,并在调用deleteItems

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

这篇关于无效的更新:第0部分中的项目数无效.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆