views:

194

answers:

3

Hi,

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...

A: 

The simplest explanation is that indexPath is nil which causes an empty array to be passed to the delete method.

Log the value of indexPath to confirm.

TechZen
For : printf("section : %d, row : %d\n", indexPath.section, indexPath.row);Here is the log :section : 1, row : 0.So no indexPath is not nil...
Leo
Probably have to look somewhere else in the code then. I don't see a problem with what you've got posted right now.
TechZen
A: 

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.

Kalle
+1  A: 

When you have only one row in section, you should delete that section.Here is following code to delete section. [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];

Apoorv