iOS 中 UITableView 中的展开/折叠部分 [英] Expand/collapse section in UITableView in iOS

查看:68
本文介绍了iOS 中 UITableView 中的展开/折叠部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在 UITableViewsections 中执行 UITableView 可展开/可折叠动画,如下所示?

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

推荐答案

您必须制作自己的自定义标题行并将其作为每个部分的第一行.子类化 UITableView 或已经存在的标题会很痛苦.根据它们现在的工作方式,我不确定您是否可以轻松地从它们中获取操作.您可以将单元格设置为看起来像标题,并设置 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.

这篇关于iOS 中 UITableView 中的展开/折叠部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆