我正在尝试使用父子自定义 tableview 单元格实现 Accordion tableview。我正在使用下面提到的开源代码。
源代码:https://github.com/singhson/Expandable-Collapsable-TableView
在该代码中,具有单个表格 View 和单个表格 View 单元格。它将显示父单元格和子单元格,但我想制作:
- 主要 Storyboard TableView
- Parenttableviewcell分离类和xib
- Childtableviewcell分离类和xib
它应该应用于带有 Accordion 的主 Controller TableView 。现在在此代码中没有单独的自定义单元格(父级和子级使用相同的 tableview 单元格并且仅更改数据)。
最佳答案
您可以通过尝试管理表格 View 的部分和行来实现它,这是我认为无需使用任何第三方代码即可实现它的最简单方法。例如
- 所有节标题都将包含自己的行数。
- 采用一个数组或字典或数组来存储部分展开行的状态。 (对展开状态说“1”,对折叠状态说“0”)
- 如果在初始状态下,所有 UI 都已展开,则将数组或字典中的所有对象设置为“1”。
- 点击各个标题(我假设部分将在单击部分的各个标题时折叠),您可以了解需要折叠的部分。对于数组或字典中的折叠部分行,将此状态保留为“0”。
- 重新加载表格并检查数组或字典中“0”实体的 heightForRow 委托(delegate)方法。无论您在哪里发现它为“0”,都在委托(delegate)方法中将高度返回为 0。
- 为反向功能执行相反的操作。
代码更新
- (IBAction)btnMenuViewTypeTapped:(id)sender{
UIButton *btnSender = (UIButton *)sender;
if(menuViewType == MVTCollapse){
[btnSender setImage:[UIImage imageNamed:@"MenuCollapse"] forState:UIControlStateNormal];
menuViewType = MVTExpand;
for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
[mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"1"];
}
} else{
[btnSender setImage:[UIImage imageNamed:@"MenuExpand"] forState:UIControlStateNormal];
menuViewType = MVTCollapse;
for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
[mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"0"];
}
}
[tblViewRestaurantMenu reloadData];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [mutArrSearchMenuItems count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger intNumOfRow;
if([mutArrSectionOpened[section] isEqualToString:@"1"]){
intNumOfRow = [mutArrSearchMenuItems[section][strMenuType] count];
}
else{
intNumOfRow = 0;
}
return intNumOfRow;
}
关于ios - 如何使用 iOS Storyboard创建具有两个嵌套 tableview 单元格 xib 的单个 tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194149/