将项目从一个 UICollectionView 部分移动到另一个部分,然后删除该项目时发生崩溃

Crash when moving item from one UICollectionView Section to another, then deleting that item

我有一个包含两个部分的 collectionView,每个部分都用单独的数组填充单元格。

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if section == 0 && GlobalList.priorityArray.count != 0 {

        return GlobalList.priorityArray.count

    } else if section == 1 && GlobalList.listItemArray.count != 0 {

        return GlobalList.listItemArray.count

    } else {
        return 0
    }
}

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

我可以毫无问题地在各个部分之间移动项目。我也可以删除项目。当我在部分之间移动项目、重新加载数据,然后尝试删除该部分中的所有项目时,出现了我的问题,我收到以下错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

这是我创建的用于处理删除和移动项目的方法

移动项目:

    override func collectionView(_ collectionView: UICollectionView,
                             moveItemAt sourceIndexPath: IndexPath,
                             to destinationIndexPath: IndexPath) {

    if (sourceIndexPath as NSIndexPath).section == 0 {

        listItem = GlobalList.priorityArray.remove(at: sourceIndexPath.item)

    } else if (sourceIndexPath as NSIndexPath).section == 1 {

        listItem = GlobalList.listItemArray.remove(at: sourceIndexPath.item)

    }

    if (destinationIndexPath as NSIndexPath).section == 0 {

        GlobalList.priorityArray.insert(listItem, at: destinationIndexPath.item)

    } else if (destinationIndexPath as NSIndexPath).section == 1 {

        GlobalList.listItemArray.insert(listItem, at: destinationIndexPath.item)

    }

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(reloadData), userInfo: nil, repeats: false)

}

正在删除项目:

    func deleteItem(sender: UIButton) {

    let point : CGPoint = sender.convert(.zero, to: collectionView)
    let i = collectionView!.indexPathForItem(at: point)
    print("deleting.. \(String(describing: i))")
    let index = i
    GlobalList.listItemArray.remove(at: (index?.row)!)
    deleteItemCell(indexPath: i!)

}

func deleteItemCell(indexPath: IndexPath) {

    collectionView?.performBatchUpdates({
        self.collectionView?.deleteItems(at: [indexPath])
    }, completion: nil)

}

让我知道是否还需要解决这个问题。

提前致谢!

我在调用 reloadData 时也遇到了与 collectionView 类似的问题,然后在尝试将单元格插入 collectionView 后直接调用。我通过在 collectionView 中手动删除和插入部分来解决这个问题,执行批量更新,如下所示:

collectionView.performBatchUpdates({ [weak self] () in
    self?.collectionView.deleteSections([0])
    self?.collectionView.insertSections([0])
}, completion: nil)