【问题标题】:Invalid update: invalid number of items in section 0无效更新:第 0 节中的项目数无效
【发布时间】: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 = []
}

【问题讨论】:

标签: ios swift xcode uicollectionview drag-and-drop


【解决方案1】:

我实际上只是删除了 performBatchUpdates 方法,这个问题就解决了。 原因的解释在这里: https://fangpenlin.com/posts/2016/04/29/uicollectionview-invalid-number-of-items-crash-issue/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多