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

时间:2017-07-03 01:56:26

标签: 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)

}

如果需要其他任何东西来解决这个问题,请告诉我。

提前致谢!

1 个答案:

答案 0 :(得分:3)

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

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