1

I've an error when I try to delete a row from a tableView. The code goes like the following

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
 if editingStyle == UITableViewCellEditingStyle.Delete {

     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     tableView.reloadData()

 }

}

The error printed from Xcode is the following:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).

I've read here on Stack this error but I can't figure it out. :(

Thanks in advance.

1 Answer 1

3

You deleted the row from the table, but first you need to delete the corresponding datum from your data model. For example, if your data model is an array, then you might call removeAtIndex on it to remove that row. You must always proceed in this order: adjust the data model, adjust the table view.

(There is no need to reload the data at this time; you can omit that line.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yeah I've to removeAtIndex on the array then remove from the tableView itself. It makes sense. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.