I have a UITableView which uses a singleton object as a datasource.
I have implemented the following methods to allow a user to delete the rows in the UITableView. But when I click the delete button the app crashes with a exception
Methods:
-(void)viewDidLoad
{
some initialization code-----
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
}
-(IBAction)toggleEdit:(id)sender
{
[self.myOrderTable setEditing:!self.myOrderTable.editing animated:YES];
if(self.myOrderTable.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
{
[self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
}
}
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row = [indexPath row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
However I get the following exception when I try to click on the delete button:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0 CoreFoundation 0x010305a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01184313 objc_exception_throw + 44
2 CoreFoundation 0x00fe8ef8
+[NSException raise:format:arguments:] + 136
3 Foundation 0x001153bb -
[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00398e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420
5 UIKit 0x00387cf8 -
[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
6 Restaurant 0x000117f9 -
[MyOrderViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 114
7 UIKit 0x00385037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
8 UIKit 0x0031a4fd -[UIApplication sendAction:to:from:forEvent:] + 119
9 UIKit 0x003aa799 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x003acc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11 UIKit 0x003ab7d8 -[UIControl touchesEnded:withEvent:] + 458
.............
..........
.............
)
terminate called after throwing an instance of 'NSException'
Where I can begin to solve this problem?