在iOS中的UITableView中展开/折叠部分

问题描述:

有人可以告诉我在$ code的部分中执行 UITableView 可扩展/可折叠动画的方法> UITableView 如下所示?

Could somebody tell me the way to perform UITableView expandable/collapsible animations in sections of UITableView as below?

你必须制作自己的自定义标题行并将其作为第一行每节的一行。对 UITableView 或已经存在的标题进行子类化将会非常痛苦。根据他们现在的工作方式,我不确定您是否可以轻松地采取行动。您可以将单元格设置为LOOK,如标题,并设置 tableView:didSelectRowAtIndexPath 以手动展开或折叠它所在的部分。

You have to make your own custom header row and put that as the first row of each section. Subclassing the UITableView or the headers that are already there will be a pain. Based on the way they work now, I am not sure you can easily get actions out of them. You could set up a cell to LOOK like a header, and setup the tableView:didSelectRowAtIndexPath to manually expand or collapse the section it is in.

我会存储一系列布尔值,这些布尔值对应于每个部分的消耗值。然后,您可以在每个自定义标题行上使用 tableView:didSelectRowAtIndexPath 切换此值,然后重新加载该特定部分。

I'd store an array of booleans corresponding the the "expended" value of each of your sections. Then you could have the tableView:didSelectRowAtIndexPath on each of your custom header rows toggle this value and then reload that specific section.

- (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 +项目中的项目数。

Then set numberOfRowsInSection to check the mybooleans value and return 1 if the section isn't expanded, or 1+ the number of items in the section if it is expanded.

- (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 返回任何部分第一行的自定义标题单元格。

Also, you will need to update cellForRowAtIndexPath to return a custom header cell for the first row in any section.