UITableView Sectioned Row Question
I have a sectioned UITableView using dictionaries adapting code from a tutorial (I don't have a link on me at the moment but I can dig it up if you really need it).
Anyway I'm trying to have it delete an开发者_如何学Go item from the list when I get this error (warning: its long)
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 (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).
I Kind of get the information about the number of rows but I'm nut sure how to fix this.
If you want to see the debugger information I paste-binned it because its so long: http://pastebin.com/0difSEnM
Here's my code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)index {
NSDictionary *dictionary = [data objectAtIndex:index.section];
NSMutableArray *array = [NSMutableArray arrayWithArray:[dictionary objectForKey:@"My Wishlists"]];
NSString *selectedWishlist = [array objectAtIndex:index.row];
//now remove the item
[array removeObjectAtIndex:index.row];
[data removeObjectAtIndex:index.section];
NSDictionary *newdictionary = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:array] forKey:@"My Wishlists"];
[data insertObject:newdictionary atIndex:0];
// [data addObject:newdictionary];
[mainTableView reloadData]; // tried without and with this, no difference
[mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationTop];
}
Thanks! Christian Stewart
Looks like this is your problem:
[data insertObject:newdictionary atIndex:0];
From the docs for NSMutableArray's insertObject:atIndex:
:
Discussion If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.
Try using replaceObjectAtIndex:withObject:
instead.
精彩评论