NSInternalInconsistencyExceptionException tableView行删除 [英] NSInternalInconsistencyException tableView row deletion

查看:62
本文介绍了NSInternalInconsistencyExceptionException tableView行删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 3.0 iOS 10.x

Swift 3.0 iOS 10.x

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

Using this code to try and delete a row in a table...

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)


    }
}

此操作失败并显示错误消息?

This fails with the error message?

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).'

哪个听起来很合理,但这只是样板代码?除了点击红色按钮,我还要做些其他事情?

Which sounds fair enough only this is boiler plate code? I am doing little more than clicking on the red button?

是的,这里有3行...在我崩溃的代码中有9行。

Yes, there are 3 rows here... in my code that crashed there were 9.

我在这里错过了什么?在这里打印出返回的indexPath,的确确实是错误的,但是请等待我没有设置它。此方法有效吗?

What have I missed here? Printed out the returned indexPath here and indeed it was wrong, but wait I didn't set it. This method did?

删除[0,3]

推荐答案

您必须先删除数据数组中的行,然后再删除 tableView

You must delete row in your data array before deleting in 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)
    }
}

这篇关于NSInternalInconsistencyExceptionException tableView行删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆