crash on deleteRowsAtIndexPaths

I've seen this before and it is definitely that you have forgotten to update source of data which fill up table. But that part of code is missing. The method which caused this is


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ;
}


Yes, the problem was in method numberOfRowsInSection So, to leave troubles you should:

  1. In function commitEditingStyle delete data from your array, database etc.
  2. Decrement your current row count.
  3. [tableView beginUpdates];
  4. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];

  5. [tableView endUpdates];

And all be OK!


Updated Swift 4.2

If you are about to remove last row in section, you should remove entire section instead of row. To do that:

    tableView.beginUpdates()
    dataArray.remove(at: row)
    if tableView.numberOfRows(inSection: section) == 1 {
        tableView.deleteSections(IndexSet(integer: section), with: .automatic)
    }
    else {
        tableView.deleteRows(at: indexPath, with: .fade)
    }
    tableView.endUpdates()

Pretty clear from the error message:

'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

The object you are trying to delete doesn't get deleted in your datasource.
If I assume that your UITableViewDataSource uses dbManager too, you have to fix dbManager to actually delete the object.


I got the same error and reason was I delete the last row in the table and my numberOfSectionsInTableView implementation return rowsArray count which was 0..

changing to minimum of 1 solve the case