iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – uitableview:嵌套节标题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用以下结构实现uitableview:

>第0组

>第0节

>单元格0
>细胞1
>细胞2

>第1节

>单元格0
>细胞1
>细胞2

>第1组

>第0节

>单元格0
>细胞1
>细胞2

>第1节

>单元格0
>细胞1
>细胞2

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

所以总是可以看到两个部分.

我该如何实现?或者有人已经实现了这个吗?

谢谢 :)

解决方法

嵌套部分的技巧是在表视图中有两种行.一个表示第二级部分,另一个表示tableview中的正常行.假设您有一个两级数组(比如部分)来表示表视图中的项目.

然后,我们拥有的部分总数只是顶级部分的数量.每个顶级部分中的行数将是子部分的数量,即每个子部分中的行数.

- (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关于如何做到这一点.

大佬总结

以上是大佬教程为你收集整理的ios – uitableview:嵌套节标题全部内容,希望文章能够帮你解决ios – uitableview:嵌套节标题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。