如何在UItableview下创建一个带UItableview的Accordion?

Ash*_*osh 4 iphone accordion uitableview ios

我正在开发应用程序,需要Facebook/Gmail iphone应用程序类型菜单,我已经在DDMenuController的帮助下.但是现在我有一个要求,其中一行需要在点击时显示带有5行的另一个tableview(所有应该是可点击的并且能够推送新的viewcontrollers).我已经看过几个代码示例,但似乎没有任何东西符合我的要求,所以只是试图把它放在这里希望有人有更好的解决方案.

谢谢,

ano*_*eal 12

我的一项工作要求有一个手风琴视图,但有多个级别的扩展和单元格崩溃,如打开/关闭目录结构.

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

这里的代码是Accordion_git

更新 了这个SWIFT版本,不确定是否最佳但是有效.

代码在这里Accordion_SWIFT

手风琴

  • 对共享网站来说,这是一个糟糕的选择.. (2认同)
  • @Rizon改为git :) (2认同)

Sha*_* TK 8

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

这里有好的教程

您可以在此处下载示例代码

示例代码

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

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)