Arrays 任务:删除:)

Arrays 任务:删除:),arrays,swift,uitableview,animation,Arrays,Swift,Uitableview,Animation,我在下面的代码中尝试删除行。当行不是节中的最后一行时,一切正常,但当它是节中的最后一行时,应用程序崩溃 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { if let item = shopList.getItem(in

我在下面的代码中尝试删除行。当行不是节中的最后一行时,一切正常,但当它是节中的最后一行时,应用程序崩溃

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        if let item = shopList.getItem(index: indexPath) {
            shopTableView.beginUpdates()

            let indexSet = NSMutableIndexSet()
            indexSet.add(indexPath.section - 1)
            shopTableView.deleteRows(at: [indexPath], with: .fade)
            //shopTableView.deleteSections(indexSet as IndexSet, with: UITableViewRowAnimation.automatic)



            shopList.remove(item: item)

            shopTableView.endUpdates()

        }
    }
}
我出错了

'NSInternalInconsistencyException',原因:'无效更新:无效>节数。更新(1)后的表视图>中包含的节数必须等于更新(2)前的表视图>中包含的节数,加上或减去>插入或删除的节数(插入0,删除0)。'


有标准的方法吗?

我在下面更改了我的代码,它可以正常工作

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        if let item = shopList.getItem(index: indexPath) {
            shopTableView.beginUpdates()
            let sectionStatus = shopList.remove(item: item)
            shopTableView.deleteRows(at: [indexPath], with: .fade)
            if sectionStatus == .sectionEmpty  {
                let indexSet = IndexSet(integer: indexPath.section)
                shopTableView.deleteSections(indexSet, with: UITableViewRowAnimation.automatic)
            }
            shopTableView.endUpdates()
            totalLabel.update(value: shopList.total)

        }
    }

}