gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:30:59 26 4
gpt4 key购买 nike

我有一个包含两个部分的 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)

关于ios - 将项目从一个 UICollectionView 部分移动到另一个部分,然后删除该项目时发生崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44876790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com