我在从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];
}
知道这里有什么问题吗?
答案 0 :(得分:1)
从阵列中删除对象后,需要调用reloadData
。当您调用reloadData
方法时,您的新数组会更新,以便UITableView
根据新的更新数据加载新单元格。