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

标签 ios swift3 uicollectionview uicollectionviewcell

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

相关文章:

iOS - 异步请求和 UI 交互

ios - 无法快速获取 View Controller 的索引,该 View Controller 是 UINavigationController 的 View Controller

iphone - 为什么我更愿意为每个新线程或 NSOperation 创建一个 NSManagedObjectContext 而不是在主线程上调用核心数据?

ios - Auth.auth().createUser(withEmail : emailTextField. text!, password : passwordTextField. text!, completion : { (user: User? , error : Error? )

iOS 功能和按钮

ios - 崩溃”无法转换为“LLAppDelegateProxy”类型的值”

ios - UICollectionView 在 scrollToItemAtIndexPath 之后水平滚动 :

swift - 如何将 UICollectionViewCell 的背景图像获取到旋转时的单元格的spectFit?

objective-c - 为什么我的 UIScrollView 在为我的 ScrollView 的 subview 的每个 UIImage 添加图层阴影后变得渲染缓慢?

c++ - 为 iOS 编译 OpenCV 失败