更新无效:第0部分中的项目数无效

问题描述

insertItems(at :)deleteItems(at :)的调用也必须伴随数据源的更改。

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

解决方法

最近我收到以下错误:

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

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

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

我在集合视图中仅使用了一个部分:

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

我已经检查了关于同一主题的几篇文章,但是它们并没有解决我的问题,我的猜测是问题出在以下两行中,但是我不确定:

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

请帮忙。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...