1

我是使用 XCode 和 Objective-C 开发的新手,希望你能帮助我。

问题是,我有一个带有 UITableView 的 UITableViewController(使用 InterfaceBuilder 创建)。

部分标题下的单元格是可展开的。

现在我想在现有的TableView下动态创建多个UITableView。

样式将与现有 TableView 的样式相同。

你能告诉我如何以编程方式创建这些 TableView 吗?

非常感谢你

迈克尔

4

1 回答 1

0

从你所说的尝试使用分组表视图。查看此链接以获得快速概览,然后转到分组表视图部分。

编辑在这里找到了这个例子:

好像这就是你要找的东西。还有一个很酷的想法。

您只需制作自己的自定义标题行并将其作为每个部分的第一行即可。子类化 UITableView 或现在在那里的标题可能会是一个巨大的痛苦,我不确定你是否可以像现在这样轻松地从它们中获取操作。您可以轻松地将单元格设置为看起来像标题,并设置tableView:didSelectRowAtIndexPath手动展开或折叠它所在的部分。

如果我是你,我会存储一个布尔数组,对应于你每个部分的“消耗”值。然后,您可以tableView:didSelectRowAtIndexPath在每个自定义标题行上切换此值,然后重新加载该特定部分。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ///it's the first row of any section so it would be your custom section header

        ///put in your code to toggle your boolean value here
        mybooleans[indexPath.section] = !mybooleans[indexPath.section];

        ///reload this section
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }
}

然后,您将设置您的数字numberOfRowsInSection以检查该mybooleans值,如果该部分未展开,则返回 1,如果该部分已展开,则返回 1+ 该部分中的项目数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (mybooleans[section]) {
        ///we want the number of people plus the header cell
        return [self numberOfPeopleInGroup:section] + 1;
    } else {
        ///we just want the header cell
        return 1;
    }
}

您还必须更新您cellForRowAtIndexPath以返回任何部分中第一行的自定义标题单元格。

于 2012-08-15T14:55:30.603 回答