当我尝试删除行以模拟折叠 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;
}
}
expandArray 是一个 bool 数组,它们都被初始化为 YES,因为所有部分都是默认扩展的。
编辑:我添加了一些调试消息,并收到以下内容。
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 节时被折叠了。