【问题标题】:iPhone UITableView Nested SectionsiPhone UITableView 嵌套部分
【发布时间】:2011-05-19 12:52:06
【问题描述】:

对于acani iPhone 应用程序,我想在UITableView 中显示组(基于兴趣)。我想按分类组织这些组,例如:

  • 运动
    • 球棒
      • 棒球
      • 垒球
      • 板球
    • 曲棍球
      • 曲棍球
      • 冰球
      • 曲棍球
  • 工程
    • 电气工程
    • 生化工程

我应该如何在UITableView 上安排这个?

我认为应该有一个根 UITableView 将具有运动与工程部分,并且细胞蝙蝠和球和曲棍球将在运动部分下,细胞电气工程和生化工程将在工程部分下。

那么 Bat-and-ball 应该有自己的 UITableView,其中应该有 Baseball、Softball 和 Cricket 的单元格。

这听起来像是安排 UI 的好方法吗?

对于这样的 UI,您有任何示例代码或 Xcode 示例代码的链接吗?必须有一个 Xcode 示例项目来做这样的事情。也许是元素周期表项目或核心数据手册?

谢谢!

马特

【问题讨论】:

    标签: iphone uitableview user-interface ios


    【解决方案1】:

    你明白了。 UITableView 实际上并不是为了显示超过两个层次的层次结构,如节和行。如果您想显示两个以上的级别,大多数(所有?)iOS 应用程序中使用的“向下钻取”方法,其中点击一行会在导航堆栈上显示另一个 UITableView。 (如你所说。)

    有很多 Apple 示例代码项目使用这种设计模式。

    编辑:刚刚检查过,DrillDownSave 就是一个很好的例子,SimpleDrillDown 也是。

    【讨论】:

    • 是的,这也是我推荐的。
    • @Shaggy Frog 有 swift 3 版本吗?
    【解决方案2】:

    嵌套部分的诀窍是在表格视图中有两种类型的行。一个代表第二级部分,另一个代表表格视图中的正常行。假设您有一个两级数组(例如部分)来表示表格视图中的项目。

    那么,我们拥有的部分的总数就是顶级部分的数量。每个顶级部分的行数将是子部分的数量 + 每个子部分的行数。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.sections.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSArray *sectionItems = self.sections[(NSUInteger) section];
        NSUInteger numberOfRows = sectionItems.count; // For second level section headers
        for (NSArray *rowItems  in sectionItems) {
            numberOfRows += rowItems.count; // For actual table rows
        }
        return numberOfRows;
    }
    

    现在,我们需要考虑的是如何为表格视图创建行。在情节提要中设置两个具有不同重用标识符的原型,一个用于节标题,另一个用于行项,然后根据数据源方法中的询问索引实例化正确的一个。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
        NSMutableArray *sectionHeaders = self.sectionHeaders[(NSUInteger) indexPath.section];
        NSIndexPath *itemAndSubsectionIndex = [self computeItemAndSubsectionIndexForIndexPath:indexPath];
        NSUInteger subsectionIndex = (NSUInteger) itemAndSubsectionIndex.section;
        NSInteger itemIndex = itemAndSubsectionIndex.row;
    
        if (itemIndex < 0) {
            // Section header
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SECTION_HEADER_CELL" forIndexPath:indexPath];
            cell.textLabel.text = sectionHeaders[subsectionIndex];
            return cell;
        } else {
            // Row Item
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ROW_CONTENT_CELL" forIndexPath:indexPath];
            cell.textLabel.text = sectionItems[subsectionIndex][itemIndex];
            return cell;
        }
    }
    
    - (NSIndexPath *)computeItemAndSubsectionIndexForIndexPath:(NSIndexPath *)indexPath {
        NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
        NSInteger itemIndex = indexPath.row;
        NSUInteger subsectionIndex = 0;
        for (NSUInteger i = 0; i < sectionItems.count; ++i) {
            // First row for each section item is header
            --itemIndex;
            // Check if the item index is within this subsection's items
            NSArray *subsectionItems = sectionItems[i];
            if (itemIndex < (NSInteger) subsectionItems.count) {
                subsectionIndex = i;
                break;
            } else {
                itemIndex -= subsectionItems.count;
            }
        }
        return [NSIndexPath indexPathForRow:itemIndex inSection:subsectionIndex];
    }
    

    Here's a detailed post 了解如何执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多