开发者

Tableview crash when deleting row?

I am having a crash with deleting a cell from a tableview. I am using a NSMutableArray loaded from NSUserDefaults to load the cells into the tableview. This is the crash: Code:

2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046
2011-09-03 18:21:06.100 App[10356:707] *** 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 (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or min开发者_Go百科us the number of rows moved into or out of that section (0 moved in, 0 moved out).'

This is how I delete the rows... Code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}

This is my numberOfRowsInSection delegate method: Code: // Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.cellArray = [NSMutableArray array];
    [self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"cellArray"]];

    if ([self.cellArray count] == 0) {
        [ivstatstableView setHidden:YES];
        [sortbar setEnabled:NO];
    }
    return [cellArray count];
}

Any idea what is wrong here?


You need to call reloadData after removing an object from your array. When you call reloadData method, your new array is updated which lets your UITableView load new cells based on the new updated data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜