删除行时Tableview崩溃?

时间:2011-09-03 22:45:23

标签: ios crash tableview

我在从tableview中删除一个单元格时遇到了崩溃。我使用从NSMutableArray加载的NSUserDefaults将单元格加载到tableview中。 这是崩溃: 代码:

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 minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这就是我删除行的方法...... 代码:

- (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"];
}

这是我的numberOfRowsInSection委托方法: 码: //自定义表视图中的行数。

- (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];
}

知道这里有什么问题吗?

1 个答案:

答案 0 :(得分:1)

从阵列中删除对象后,需要调用reloadData。当您调用reloadData方法时,您的新数组会更新,以便UITableView根据新的更新数据加载新单元格。

相关问题