【问题标题】:Crash when moving item from one UICollectionView Section to another, then deleting that item将项目从一个 UICollectionView 部分移动到另一个,然后删除该项目时崩溃
【发布时间】:2023-03-03 19:09:01
【问题描述】:

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

}

让我知道是否需要其他任何东西来解决这个问题。

提前致谢!

【问题讨论】:

  • 尝试直接调用self.collectionView?.deleteItems(at: [indexPath]),而不是在执行批量更新中。

标签: ios swift3 uicollectionview uicollectionviewcell


【解决方案1】:

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

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

【讨论】:

  • 这可以用来删除项目吗,我看到你提到了部分?
  • 是的,只是在调用它之前更新数据源,它应该删除它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多