ios 如何在 UItableview 下创建带有 UItableview 的 Accordion?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15442697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an Accordion with UItableview under a UItableview?
提问by Ashutosh
I am working on app which requires a Facebook/Gmail iphone app type menu which i already have with the help of DDMenuController. But now i have requirement where one of the rows needs to show accordion on click with another tableview with 5 rows (all should be clickable and able to push new viewcontrollers). I have seen couple of code sample but nothing seems to fit my requirement, so just trying to put it out here hoping someone has a better solution.
我正在开发需要 Facebook/Gmail iphone 应用程序类型菜单的应用程序,我已经在 DDMenuController 的帮助下拥有了该菜单。但是现在我有一个要求,其中一行需要在单击时显示手风琴,另一个表视图有 5 行(所有行都应该是可点击的并且能够推送新的视图控制器)。我看过几个代码示例,但似乎没有什么符合我的要求,所以只是想把它放在这里希望有人有更好的解决方案。
Thanks,
谢谢,
回答by anoop4real
One of my work had a requirement to have an accordion view but having multiple levels of expansion and collapse of cells like you open/ close your directory structure.
我的一项工作要求有一个手风琴视图,但有多个级别的单元格展开和折叠,就像打开/关闭目录结构一样。
I did a sample on that, I was able to achieve the desired result. The basic concept is the same, I am using the deleteRowsAtIndexPath and insertRowsAtIndex path only but by creating a model object which has a parent children relationship and loading the children into the main array whenever the parent is tapped. I am not good at putting a tutorial so I am sharing my sample code, hope it helps someone.
我做了一个样本,我能够达到预期的结果。基本概念是相同的,我只使用 deleteRowsAtIndexPath 和 insertRowsAtIndex 路径,但是通过创建一个具有父子关系的模型对象,并在父项被点击时将子项加载到主数组中。我不擅长放置教程,所以我分享了我的示例代码,希望对某人有所帮助。
Code here Accordion_git
此处代码Accordion_git
UpdatedDid a SWIFT version of this, not sure whether optimal but it works.
更新了这个的 SWIFT 版本,不确定是否最佳,但它有效。
Code here Accordion_SWIFT
此处代码Accordion_SWIFT
回答by Shamsudheen TK
Better solution is Expand or CollapseTableView Sections
更好的解决方案是展开或折叠TableView 部分
Good Tutorial is available here
好的教程可以在这里找到
You can download the sample code here
您可以在此处下载示例代码
Sample Code
示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = @"Expandable"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = @"Some Detail";
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else
{
cell.accessoryView = nil;
cell.textLabel.text = @"Normal Cell";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
}
}