Deleting from UITableView and supporting data source in iOS 4
I'm a bit stumped by this after reading over the Apple documentation and searching The Googles. The gist of the problem is I know I need to delete from the UITableView and the datasource (NSMutableArray in this case) but it won't let me do either regardless of the order I do it in.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myMutableObjectList removeObjectAtIndex:indexPath.row];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// not used
}
}
Regardless of which order I use for these two:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myMutableObjectList removeObjectAtIndex:indexPath.row];
It either complains about how many elements are left or says the deletion did not occur.
"(Count) before deletion must match (count) after d开发者_如何学Goeletion minus however many you've deleted 0 insertions 1 deletion"
With [tableView....] first it says the count is the same with 1 deletion wanting to happen.
With [myMutableObjectList...] first it says count is 1 less (good!) but 0 deletion wanting to happen.
If I can't have it either way, what am I supposed to do?
Updates:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myMutableObjectList count] == 0) {
return section;
} else {
return 4; //4 variables in myObject
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"Size of list: %d", [myMutableListList count]);
if([myMutableList count] == 0) {
return 100;
} else {
return [myMutableList count];
}
}
Your numberOfRowsInSection doesn't match the behavior you're going for. It never will reflect changes to your data store.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myMutableObjectList count] == 0) {
return section;
} else {
return 4; //4 variables in myObject
}
}
Why are you returning section here. I'd imagine you want to return [myMutableObjectList count]
This method needs to reflect the CURRENT count of all the cells. When you do your deletion, you remove an object from myMutableObjectList
but that change wouldn't affect the result of your numberOfRows method.
精彩评论