开发者

iPhone-App crashes when a row of UITableView is deleted

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) _sendActionsForEve开发者_StackOverflownts: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?


"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).'"

The error clearly says what's wrong. I would examine your numberOfRowsInSection method. You need to account for the row being deleted. You can't always return the same number of rows. If you're deleting one row from the table, your numberOfRowsInSection needs to be returning it's old return value minus 1.

For example:

Let's say I use an array of strings to populate the tableview;

My numberOfRowsInSection method would use

return [array count];

My cellForRowAtIndexPath method would use

cell.textLabel.text = (NSString*)[array objectAtIndex:indexPath.row];

When I delete a row (say row number 3) from the tableView, I would remove the object from the array as well:

[array removeObjectAtIndex:3];

This way when numberOfRowsInSection is called again, the array is one object smaller and numberOfRowsInSection returns one number less than before, which accounts for the deleted row.


Did you read the error message? It says:

reason: 'Invalid update: invalid number of rows in section 1.

So... the number of rows in your tableView is now wrong. Why?

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

When you delete a row from the tableView, you also have to delete the corresponding object from the array of objects that get shown in the tableView. You didn't do that. Thus, UITableView threw an exception.


Are you removing an item from your datasource in commitEditingStyle:? If the table datasource has 2 items and you delete a row in commitEditingStyle: you should also remove an item from your datasource so that now the tableview and the datasource both have 1 element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜