【发布时间】:2019-03-23 02:23:56
【问题描述】:
我在尝试将项目从一个 UICollectionView (collectionView) 拖到另一个 (rackView) 时遇到以下错误:
'NSInternalInconsistencyException',原因:'无效更新:第 0 节中的项目数无效。更新 (7) 后现有节中包含的项目数必须等于该节之前包含的项目数更新(6),加上或减去从该部分插入或删除的项目数(0 插入,0 删除),加上或减去移入或移出该部分的项目数(0 移入,0 移出)。 '
奇怪的是,当我将项目从 rackView 拖到 collectionView 时,几乎完全相同的代码适用于拖放。当从机架数据源中删除项目时,rackView 会正确更新,但当从板 (collectionView) 将项目添加回其中时会崩溃。有谁知道为什么会这样,有什么办法可以解决吗?
感谢您提供的任何信息。
我尝试添加一个变量来表示机架中的计数,并在 numberOfItemsInSection 方法中返回它。这在其他地方被声明为此错误的可能解决方案,但似乎无法为我解决。
private func moveItemsFromRack(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
{
collectionView.performBatchUpdates({
for (index, item) in coordinator.items.enumerated()
{
let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
self.board[indexPath.row] = item.dragItem.localObject as! String
self.rack.remove(at: self.sourceIndex.row)
}
DispatchQueue.main.async {
collectionView.reloadItems(at: [destinationIndexPath])
self.rackView.reloadData()
}
})
self.sourceIndex = []
}
private func moveItemsFromBoard(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
{
collectionView.performBatchUpdates({
for (index, item) in coordinator.items.enumerated()
{
let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
self.rack.insert(item.dragItem.localObject as! String, at: indexPath.row)
self.board[self.sourceIndex.row] = ""
}
DispatchQueue.main.async {
collectionView.reloadItems(at: [self.sourceIndex])
self.rackView.reloadData()
}
})
self.sourceIndex = []
}
【问题讨论】:
-
您可以使用差异算法。请查看以下链接:medium.com/flawless-app-stories/…
-
感谢 Victor,我今天会考虑实施。
标签: ios swift xcode uicollectionview drag-and-drop