首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone UITableView嵌套节

iPhone UITableView嵌套节
EN

Stack Overflow用户
提问于 2010-12-03 03:25:30
回答 2查看 8.2K关注 0票数 7

对于阿卡尼 iPhone应用程序,我希望在UITableView中显示组(基于兴趣)。我想从分类学上组织这些团体,例如:

  • 体育
    • 球拍加球
      • 棒球
      • 垒球
      • 板球

代码语言:javascript
复制
- Hockey 
    - Field Hockey
    - Ice Hockey
    - Roller Hockey

  • 工程
    • 电气工程
    • 生化工程

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

我在想,应该有一个根UITableView,它将包括运动和工程部分,细胞球杆和曲棍球将位于体育部之下,细胞电气工程和生化工程部将在工程部项下。

然后蝙蝠和球应该有它自己的UITableView,它应该有细胞棒球、垒球和板球.

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

对于这样的UI,您有任何示例代码或Xcode示例代码链接吗?肯定有一个Xcode示例项目执行类似的操作。也许元素元素周期表项目或核心数据书籍?

谢谢!

哑光

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-03 03:37:42

你说对了。UITableView实际上并不能显示一个层次结构的两个以上级别,比如节和行。如果你想要显示两个以上的层次,一个“钻下”的方法使用在大多数(全部?)在iOS应用程序中,点击一行将显示导航堆栈上的另一个UITableView。(如你所说)

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

编辑:DrillDownSave是一个很好的例子,SimpleDrillDown也是。

票数 6
EN

Stack Overflow用户

发布于 2014-09-18 10:47:26

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

那么,我们所拥有的区段总数就是顶级部分的数量。每个顶层区段中的行数为子节数+每个分节中的行数。

代码语言:javascript
复制
- (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;
}

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

代码语言:javascript
复制
- (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];
}

这是一篇详细的文章介绍了如何做到这一点。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4342180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档