2011-09-03 103 views
0

我遇到了从tableview中删除单元格的问题。我正在使用从NSUserDefaults加载的NSMutableArray将单元加载到tableview中。 这是崩溃: 代码: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

您需要在从阵列中移除对象后调用reloadData。当您拨打reloadData方法时,您的新阵列会更新,您的新阵列可以让您的UITableView根据新的更新数据加载新的单元格。