ios - NSInternalInconsistencyException tableView 行删除

标签 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]

最佳答案

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)
    }
}

关于ios - NSInternalInconsistencyException tableView 行删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253222/

相关文章:

ios - 如何使用swift更新tableview中的数据?

swift - 如何在 swift 中将数组传递给 NSNotification

c++ - 在 Xcode 8 中加载 Fisher 人脸识别器时出现 OpenCV 错误

ios - 使用 testflight 点击按钮时应用程序崩溃

iphone - NSArray 排序和隔离

ios - 如何添加 "Load More"单元格,按下该单元格会向表格 View 添加更多行?

ios - 将 12 小时日期格式快速转换为 24 小时格式,反之亦然?

ios - swift 4 中带有文本字段和标签的自定义控件

objective-c - Xcode 4.4 中的链接器错误

ios - 当旋转到横向时,如何让 UIPageViewController 工作?