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

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

问题描述

最近报错:

<块引用>

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

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

 let removedIndexPaths = removedIndexes.map({ IndexPath(row: $0, section: 0) })让 addedIndexPaths = addedIndexes.map({ IndexPath(row: $0, section: 0) })让 updatedIndexPaths = updatedIndexes.map({ IndexPath(row: $0, section: 0) })self.collectionView?.performBatchUpdates({self.collectionView?.deleteItems(在:removedIndexPaths)self.collectionView?.insertItems(在:addIndexPaths)},完成:{ _ in守卫让collectionView = self.collectionView else {返回}对于updatedIndexPaths中的indexPath {如果让 myCell = collectionView.cellForItem(at: indexPath) as?我的CollectionViewCell {让 item = self.dataManager.items[indexPath.row]myCell.updateUI(项目)}}让 collectionViewLayout = self.collectionViewLayoutForNumberOfItems(self.dataManager.items.count)如果 collectionViewLayout.itemSize != self.collectionFlowLayout.itemSize {collectionView.setCollectionViewLayout(collectionViewLayout,动画:假)}})

我只在我的收藏视图中使用了一个部分:

override func numberOfSections(in collectionView: UICollectionView) ->诠释{返回 1}

我查看了几篇关于同一主题的帖子,但它们并没有解决我的问题,我的猜测是问题出在以下两行,但我不确定:

 self.collectionView?.deleteItems(at: removedIndexPaths)self.collectionView?.insertItems(在:addIndexPaths)

请帮忙.

解决方案

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

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

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.

解决方案

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

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

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