Skip to main content

NSRangeException when deleting cell using a different class



I looked through my code and I printed my array I was using and it appears to be fine, yet this error still persists.







* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0]'







My guess was that it had something to do with my indexPath but it doesn't make much different how much I change it.







-(void)checkboxTapped:(id)sender

{

[sender setSelected:YES];



[self.textLabel setTextColor:[UIColor grayColor]];

[self.detailTextLabel setTextColor:[UIColor grayColor]];



parent = [[ViewController alloc] init];

UITableView *tableView = parent.tableView;

NSMutableArray *array = [[NSMutableArray alloc] initWithArray:parent.array];

[parent release];



NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[array count] inSection:1];



[array removeObjectAtIndex:[indexPath row]];

[db deleteTaskAtIndex:[indexPath row]];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];



[array release];



[tableView endUpdates];

[tableView reloadData];

}




Comments

  1. In your code [indexPath row] is going to return the value of [array count]. That's unlikely to be what you want. If your array has zero objects in it, you are going to attempt to remove the object at index 0. But there will be no objects and you'll get an error. If your array has 1 object in it, you're going to attempt to remove the object at index 1. Again, that will fail, because there is no object at index 1, just one object at index 0.

    If you want to remove the last object in an array you need to use an index that is count-1. You may also need to check to see if the array is empty, if that case can occur.

    ReplyDelete

Post a Comment

Popular posts from this blog

Wildcards in a hosts file

I want to setup my local development machine so that any requests for *.local are redirected to localhost . The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local , site2.local etc, and have them all resolve to localhost , while Apache serves a different site accordingly.