【问题标题】:How to create an Accordion with UItableview under a UItableview? [closed]如何在 UItableview 下使用 UItableview 创建手风琴? [关闭]
【发布时间】:2016-05-08 15:08:52
【问题描述】:

我正在开发需要 Facebook/Gmail iphone 应用程序类型菜单的应用程序,我已经在 DDMenuController 的帮助下拥有了该菜单。但是现在我有一个要求,其中一个行需要在单击时显示手风琴,而另一个 tableview 有 5 行(都应该是可点击的并且能够推送新的视图控制器)。 我看过几个代码示例,但似乎没有什么符合我的要求,所以只是想把它放在这里,希望有人有更好的解决方案。

谢谢,

【问题讨论】:

  • 谷歌上的 5 秒带来了很多不错的教程。以this 为例。只需尝试搜索 Expanding 或 Collapsing tableviews。
  • 你可以看一下 Swift 中的这个手风琴示例:github.com/tadija/AEAccordion 创建手风琴效果的代码非常少(不是通过使用部分,而是使用单元格),另外还有一个解决方案在其他 XIB 文件中使用 XIB 文件(对于使用自定义视图的自定义单元很有用)。

标签: iphone ios uitableview accordion


【解决方案1】:

我的一个工作需要有一个手风琴视图,但有多个级别的单元格展开和折叠,就像你打开/关闭你的目录结构一样。

我做了一个样本,我能够达到预期的结果。基本概念是相同的,我只使用 deleteRowsAtIndexPath 和 insertRowsAtIndex 路径,但通过创建一个具有父子关系的模型对象,并在点击父对象时将子对象加载到主数组中。我不擅长放教程,所以我分享我的示例代码,希望它对某人有所帮助。

代码在这里Accordion_git

更新 做了一个 SWIFT 版本,不确定是否最佳,但它可以工作。

代码在这里Accordion_SWIFT

【讨论】:

  • 分享网站真是个糟糕的选择..
  • @Rizon 更改为 git :)
  • 谢谢!几年前我使用嵌套表视图实现了这个控件,但你的方法更有意义。干杯。
【解决方案2】:

更好的解决方案是展开或折叠 TableView Sections

好的教程可在here

您可以下载示例代码here

示例代码

- (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];

            }
        }
    }
}

【讨论】:

  • 这不是扩展,它是一个该死的警报大声笑
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
相关资源
最近更新 更多