0

I am getting an error when trying to delete a row from a UITableView:

Here is the data for my Table View:

var tmpArray = ["test"];

Here is how the TableView gets the number of rows:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tmpArray.count;
}

Here is the body of the method I call to remove rows:

self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);
tmpArray.remove(at: 0);

Here is the error:

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

The odd part is that there should be 1 row in the section before the row is deleted, not 2.

Any ideas as to why this error is occurring?

I have reviewed several posts, and although relevant, the solution provided here does not work: UITableView Deleteing row error

Update When I call the remove commands separately, the error still persists:

tmpArray.remove(at: 0);
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);

Error:

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 (0) must be equal to the number of rows contained in that section before the update (0), 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).'

3
  • Your update is no different. As I stated in my answer, you need to swap the two lines. Call tmpArray.remove(at:0) before you call deleteRows. Commented Apr 29, 2017 at 3:18
  • I made a mistake when I posted the update. I did reverse the lines in Xcode. Adding real code now. Commented Apr 29, 2017 at 3:19
  • Show more code. There's not enough context to what you have in your question. Commented Apr 29, 2017 at 3:50

2 Answers 2

1

Do you really need to use this line in your code?

self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);

Can't it be like updating your data source and reloading UITableView? Like:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        //1. remove data from model
        data.remove(at: indexPath.row)

        //2. reload TableView
        [tableView reloadData];

    }
}

Or try this:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        //1. remove data from model
        data.remove(at: indexPath.row)

        //2. remove row from view
        tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)




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

3 Comments

Thanks for the answer. Is there a way to animate this?
Don't reload the whole table just to delete one row.
Yah, I don't think this is the best answer
0

In func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

Below two lines should work, please maintain the order of call:

tmpArray.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);

IMHO...calling reloadData() is a costlier call here but at the end of the show its your call.

Comments

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.