在UITableViewStylePlain中展开UITableView单元格

11

我希望在表格视图中,当单击可扩展的单元格时,能够展开该单元格。其中有两个单元格是可扩展的,其他单元格则不可扩展。我需要在单击可扩展的单元格时,它应该显示其下方的单元格,并再次单击时隐藏。下面的图片定义了这一点。

enter image description here

我可以帮助您实现这个功能,请为上述内容提供指导。 谢谢。

你的问题解决了吗? - Bhavin
你有检查过我的答案吗? - Bhavin
我正在处理它。。 - iPhone Programmatically
7个回答

12

这里是带有可展开UITableView的完整教程。

以下是代码片段。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];

            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];

            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }

            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                                                               inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (currentlyExpanded)
            {
                [tableView deleteRowsAtIndexPaths:tmpArray 
                                 withRowAnimation:UITableViewRowAnimationTop];

                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];

            }
            else
            {
                [tableView insertRowsAtIndexPaths:tmpArray 
                                 withRowAnimation:UITableViewRowAnimationTop];
                cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

            }
        }
    }
}

如下图所示

enter image description here

这个这个的解决方案也可以帮助您了解如何管理可扩展的TableView。


这是群组表,我只需要一个普通的表格。 - iPhone Programmatically
@iPhone,你看过这个了吗?它适用于普通表格。 - swiftBoy
在发布问题之前,我已经看过了这样的示例。它使用了“部分展开”,而在普通情况下,我们只需要一个单元格展开。 - iPhone Programmatically
@iPhone 你可以在这里下载示例代码压缩文件(http://dl.dropbox.com/u/11290499/table.zip),该示例代码仅用于表格行扩展,希望能帮到你。祝好运! - swiftBoy
我已经按照你的第一个教程做了,并且觉得它是正确而易用的解决方案,但是当我在单元格上使用图像而不是文本以及在子菜单上使用文本时,它们重叠了。你能帮忙解决这个问题吗? - iPhone Programmatically

4

好的,我的想法是,标题“Events”,“Overhead”和“Friends”将是自定义的UIView,其中包含用于背景的UIImageView,用于标题的UILabel和用于展开的UIButton。因此,基本上

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

你已经返回标题计数。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

每个标题都返回一个UIView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

拥有该标题中行数的计数。您可能需要为此维护一个数组。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

单元格项的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

最初所有部分的值应该为0,当用户点击展开时,它应该相对于该部分中行数*单元格高度增加。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

你可能需要一些好的逻辑来设置所有行的展开。
你的展开按钮的操作类似于,
- (void) expandSection:(UIButton *)sender;

您可以使用sender.tag来识别要展开的部分,所以不要忘记正确添加标签。您可能需要在.h文件中使用int类型来存储当前部分,并且可以在- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section数据源方法中使用。


3
我认为你想要的已经由苹果公司提供了。您可以查看苹果提供的标题为“表格视图动画和手势”的示例代码

2
我已经提供了以下示例源代码。您可以根据自己的需求自定义此示例。
源代码链接:点击这里 输出屏幕:

enter image description here


2

0

接受的答案是正确的,但链接已过时。要使该示例正常工作,您需要执行额外的操作:

从此处下载ExpandableTableCells项目:
https://github.com/cocoanetics/Examples

从此处下载DTCustomColoredAccessory.h和.m文件:
https://github.com/Cocoanetics/DTFoundation/tree/develop/Core/Source/iOS

将DTCustomColoredAccessory文件放入ExpandableTableCells项目中。

这样就有一个可工作的示例了,从那里开始编辑和移动比尝试从零开始编写更容易。


我已经下载了 Download ExpandableTableCells project 并添加了 Download DTCustomColoredAccessory.h.m 文件。但是我遇到了一个错误 "error: linker command failed with exit code 1 (use -v to see invocation)",你能帮忙解决一下吗? - Luai Kalkatawi

0

通过委托,这是可能的。

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
       //write code here
    }

你曾经使用过这种方法来达到这个目的吗?你能否发布一下这个方法的示例代码? - iPhone Programmatically

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接