Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

ios - NSInternalInconsistencyException tableView row deletion

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?

enter image description here

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

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?

DELETE [0, 3]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...