NSInternalInconsistencyException tableView行删除

时间:2017-05-29 12:10:06

标签: ios swift uitableview delete-row

Swift 3.0 iOS 10.x

使用此代码尝试删除表格中的行...

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]

1 个答案:

答案 0 :(得分:2)

在删除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)
    }
}