NSInternalInconsistencyException的tableView行删除

问题描述:

雨燕3.0的iOS 10.x的NSInternalInconsistencyException的tableView行删除

使用此代码,试图删除表中的行...

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     print("DELETE \(indexPath)") 
     self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 


    } 
} 

这失败,出现错误信息?

2017-05-29 13:36:23.843228+0200[939:576777] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

这听起来不错,只有这是锅炉板代码?我只是点击红色按钮而已?

enter image description here

是的,有3列在这里......在我的代码坠毁有9

有什么我错过这里?这里打印出返回的indexPath,确实是错的,但是等一下我没有设置它。这种方法呢?

删除[0,3]

+0

你需要在数据模型中删除行以及删除之前,必须先删除你的数据阵列行。请发布您的'cellForRow'的实现,以便我可以帮到您 –

+2

您错过了从数据源数组中删除该项目(**调用'deleteRows(at')之前**。 – vadian

tableView

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     print("DELETE \(indexPath)") 
     yourArray.remove(at: indexPath.row) /* delete in data array */ 
     self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
}