如何解决更新无效:第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)
请帮忙。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。