I have a tableView with multiple sections. When a sections contains only one row, and if I try to delete that row, my app crashes while executing this code :
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
I get this exception :
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
There is an array for each section. Each one of these arrays contains objects to display in rows. Then there is another array which contains these arrays.
Any idea of what I’m doing wrong ?
Thank you.
edit :
OK, I just saw in the debugger that it’s the tableView: titleForHeaderInSection method which was crashing, because I was getting the section’s title in the array I just deleted.. So I was trying to access an array with 0 object in it…
The table view has a source, and the source contains the list of sections and rows being displayed. When you do the
[tableView deleteRowsAtIndexPaths:withRowAnimation:]call, the table view goes to update the table, but the source is now out of sync (it has the row which you just deleted). The solution is to update the source (be that an array or core data or whatever) right before updating the table view itself.