当我尝试删除行以模拟折叠UITableView中的某个部分时,收到以下错误消息:
*** 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, 3 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
我正在使用此方法删除行,其中调用者是该部分的标题视图,其标记是其部分:
- (IBAction)toggleExpand:(id)sender
{
int section = [sender tag];
if (expandedArray[section]) {
expandedArray[section] = NO;
NSMutableArray *rowsToDelete = [[NSMutableArray alloc] init];
for (int i = 0; i < 3; i++) {
[rowsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
[mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:rowsToDelete] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
似乎问题是我在删除行后没有更新我的数据源,但我试图用我的方法numberOfRowsInSection来实现:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (expandedArray[section]) {
return 3;
}
else {
return 0;
}
}
expandedArray是一个bool数组,它们都被启动为YES,因为所有部分都是通过defualt展开的。
编辑:我添加了一些调试消息,并收到以下内容。
Asking number of rows....
Section 1 is expanded.
Asking number of rows....
Section 0 is expanded.
header section: 0
header section: 1
toggleExpand tag reads: 0
Asking number of rows....
Section 0 is expanded.
Asking number of rows....
Section 1 is not expanded.
让我感到不安的是,它认为第1节在第0节时会崩溃。