【发布时间】:2017-09-10 12:45:01
【问题描述】:
最近报错:
致命异常:NSInternalInconsistencyException 无效更新:第 0 节中的项目数无效。更新后现有节中包含的项目数 (13) 必须 等于该部分中包含的项目数之前 update(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)
请帮忙。
【问题讨论】:
标签: swift swift3 uicollectionview tvos