如何在iOS中展开tableview中的节?
我对tableView中的每个部分都有一个自定义标题,headerView有一个按钮。单击该按钮,我试图通过更改该部分的行数来扩展该部分。如果我调用reloadData,它可以正常工作,但当我尝试使用reloadSections/insert/delete sections时,它会崩溃 以下是我的行数方法:如何在iOS中展开tableview中的节?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我对tableView中的每个部分都有一个自定义标题,headerView有一个按钮。单击该按钮,我试图通过更改该部分的行数来扩展该部分。如果我调用reloadData,它可以正常工作,但当我尝试使用reloadSections/insert/delete sections时,它会崩溃 以下是我的行数方法: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (highlighHeaderClicked) {
return [_expandCountArray[section]integerValue]; // 9,9,9
}
return [_collapseCountArray[section]integerValue]; //2,2,2
}
所以默认情况下tableView显示2行,当点击按钮时,我想显示9行
和按钮动作方法:
-(IBAction)highlightHeaderClicked:(id)sender{
highlighHeaderClicked = !highlighHeaderClicked;
NSIndexPath *indexPath = [self getIndexPathForView:sender];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.length-1];
[self.tableView beginUpdates];
[self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
}
通过这样做,我得到了优待:
无效更新:第2节中的行数无效。更新9之后现有节中包含的行数必须等于更新2之前该节中包含的行数,加上或减去从该节中插入或删除的行数0 inserted,0 deleted,加上或减去移入或移出该节的行数0 moved in,0 moved out
我还尝试在调用按钮操作方法时从数据源中删除对象
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_tempRowCountArray[section]integerValue]; //2,2,2
}
-(IBAction)highlightHeaderClicked:(id)sender{
highlighHeaderClicked = !highlighHeaderClicked;
NSIndexPath *indexPath = [self getIndexPathForView:sender];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.length-1];
NSInteger rowCount = _mainDataSet.dashboardArray.count;
if (rowCount > 2) {
if (highlighHeaderClicked) {
[_tempRowCountArray replaceObjectAtIndex:indexPath.length withObject:@(rowCount)];
}else{
[_tempRowCountArray replaceObjectAtIndex:indexPath.length withObject:@2];
}
[self.tableView beginUpdates];
[self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
}
}
我错过了什么?我想我每次都传递了正确的数组计数 您遇到的基本问题是,您试图插入和删除整个节。这意味着章节的数量必须改变 相反,您必须删除和插入节中的行 此外,还必须指定前一个状态和新状态之间的确切差异 例如:
NSArray *rowsToBeAdded = ...
NSArray *rowsToBeRemoved = ...
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:rowsToBeAdded withRowAnimation: UITableViewRowAnimationBottom];
[self.tableView deleteRowsAtIndexPaths:rowsToBeRemoved withRowAnimation: UITableViewRowAnimationBottom];
[self.table endUpdates];
这也意味着您必须非常小心您的逻辑,并跟踪展开的部分
对于折叠和展开的部分,必须返回正确数量的项目:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
BOOL expanded = ...;
if (!expanded) {
return 0;
}
return ...
}
这个问题的另一个解决方案是保持行始终存在,并完全避免插入和删除。可以为所有隐藏行返回零高度
要更新所有行的高度,只需调用:
[self.tableView beginUpdates];
[self.tableView endUpdates];
实现此功能的一个简单方法是保持行始终在那里,如果行被隐藏,则只返回零高度。您的高度解决方案似乎比插入行/删除行更容易。@TejaNandamuri确实如此。我会把这一点加到我的答案中,但要小心。要使“高度更改”技巧发挥作用,单元格需要启用clipsToBounds。默认情况下,它不是。@TejaNandamuri它可能会帮助您获得一个可下载的工作示例,它在Swift中,对此表示抱歉,但您可以随时翻译:@TejaNandamuri,我找到了旧的Objective-C版本: