首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >uitableview:嵌套节标题

uitableview:嵌套节标题
EN

Stack Overflow用户
提问于 2011-10-04 15:23:20
回答 3查看 6.5K关注 0票数 7

我试图用以下结构实现一个uitableview:

  • 截面组0
    • 0节
      • 细胞0
      • 细胞1
      • 细胞2

代码语言:javascript
运行
复制
- section 1 
    - cell 0
    - cell 1
    - cell 2

  • 第1组
    • 0节
      • 细胞0
      • 细胞1
      • 细胞2

代码语言:javascript
运行
复制
- section 1 
    - cell 0
    - cell 1
    - cell 2

它应该像这个截图(1-2-3-4)那样责骂:http://dl.dropbox.com/u/2213241/uitableview.png

因此,两个部分总是可见的。

我如何实现这一点?或者已经有人实现了这一点?

谢谢:)

EN

回答 3

Stack Overflow用户

发布于 2014-09-18 13:01:29

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

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

代码语言: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用户

发布于 2018-11-13 13:44:49

如果有人对上述代码的SWIFT4.2版本感兴趣,请看这里。

为了有嵌套的部分,您需要在tableView中有几种类型的行。首先表示第二个级别的节,第二个表示tableView中的标准行。假设您有一个两级数组(节)来表示tableView中的项。

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

代码语言:javascript
运行
复制
func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionItems = sections[section]
    var numberOfRows: Int = sectionItems.count // For second level section headers
    for rowItems: [Any] in sectionItems as? [[Any]] ?? [] {
        numberOfRows += rowItems.count // For actual table rows
    }
    return numberOfRows
}

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

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var sectionItems = sections[indexPath.section]
    var sectionHeaders = self.sectionHeaders[indexPath.section]
    let itemAndSubsectionIndex: IndexPath? = computeItemAndSubsectionIndex(for: indexPath)
    let subsectionIndex = Int(itemAndSubsectionIndex?.section ?? 0)
    let itemIndex: Int? = itemAndSubsectionIndex?.row

    if (itemIndex ?? 0) < 0 {
        // Section header
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "SECTION_HEADER_CELL", for: indexPath)
        cell.textLabel?.text = sectionHeaders[subsectionIndex] as? String
        return cell
    } else {
        // Row Item
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ROW_CONTENT_CELL", for: indexPath)
        cell.textLabel?.text = sectionItems[subsectionIndex][itemIndex ?? 0] as? String
        return cell
    }
}

func computeItemAndSubsectionIndex(for indexPath: IndexPath?) -> IndexPath? {
    var sectionItems = sections[Int(indexPath?.section ?? 0)]
    var itemIndex: Int? = indexPath?.row
    var subsectionIndex: Int = 0
    for i in 0..<sectionItems.count {
        // First row for each section item is header
        itemIndex = (itemIndex ?? 0) - 1
        // Check if the item index is within this subsection's items
        let subsectionItems = sectionItems[i] as? [Any]
        if (itemIndex ?? 0) < Int(subsectionItems?.count ?? 0) {
            subsectionIndex = i
            break
        } else {
            itemIndex -= subsectionItems?.count
        }
    }
    return IndexPath(row: itemIndex ?? 0, section: subsectionIndex)
}
票数 3
EN

Stack Overflow用户

发布于 2011-10-04 16:31:56

像罗宾建议的那样,子类UITableView将是一个很好的选择。我自己也做过类似的事情,但是我对UITableViewCell进行了子类化,并在其中放置了UITableView。在这种情况下,您将有一个基本的tableView,其中每个部分都是一个组。因为子类UITableViewCell,所以每一行都是它自己的UITableView,它有自己的节和行。这应该会给你你想要的外观和功能。如果你有困难的话,我很乐意帮你把它设置好,但这并不难。本教程给出了一个很好的示例,说明如何对UITableViewCell进行子类,并且是一个很好的起点。

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

https://stackoverflow.com/questions/7650243

复制
相关文章

相似问题

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