【发布时间】:2015-06-27 03:21:31
【问题描述】:
您好,我已经实现了示例Expand Table
-(void)sectionButtonTouchUpInside:(UIButton*)sender
{
// sender.backgroundColor = [UIColor greenColor];
[self.tableView beginUpdates];
int section = sender.tag;
bool shouldCollapse = ![collapsedSections containsObject:@(section)];
if (shouldCollapse)
{
int numOfRows = [self.tableView numberOfRowsInSection:section];
NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[collapsedSections addObject:@(section)];
}
else
{
int numOfRows = 0 ;
switch (section)
{
case 0:
numOfRows = (int)aArray.count;
break;
case 1:
numOfRows = (int)bArray.count;
break;
case 2:
numOfRows = (int)cArray.count;
break;
case 3:
numOfRows = (int)dArray.count;
break;
}
NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[collapsedSections removeObject:@(section)];
}
[self.tableView endUpdates];
}
我的程序在上面的代码中崩溃并显示此消息:
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 2 节中的行数。 更新后的现有节(1)必须等于 更新 (1) 之前该部分中包含的行,加号或减号 从该部分插入或删除的行数(0 插入, 1 已删除)并加上或减去移入或移出的行数 该部分(0 移入,0 移出)。
更新
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(![_collapsedSections containsObject:@(section)])
{
return 0;
}
else
{
int count = 0;
switch (section)
{
case 0:
count = (int)aArray.count;
break;
case 1:
count = (int)bArray.count;
break;
case 2:
count = (int)cArray.count;
break;
case 3:
count = (int)dArray.count;
break;
}
return count;
}
}
【问题讨论】:
-
我不知道这个程序将如何从给定的数组中删除行以及如何再次填充以显示它们。
-
移动 [collapsedSections addObject:@(section)]; & [collapsedSections removeObject:@(section)];在从表中插入或删除行之前。
-
您面临的错误是由于您的模型没有更新。这里的模型是你的数组。每当您想删除一行时,您需要先从数组中删除该特定对象,然后调用 tableView 删除函数。由于任何画面更新都会调用 numberOfSections、numberOfRows 和 cellForRowAtIndexPath 方法。
-
根据你的方法更新cl.ly/ajsi
标签: ios uitableview