【问题标题】:Expand UITableView cell in UITableViewstylePlain在 UITableViewstylePlain 中展开 UITableView 单元格
【发布时间】:2013-02-13 02:00:13
【问题描述】:

我想在点击时展开 tableview 单元格。有一个表格视图,其中两个单元格是可扩展的,而其他的则不是。我需要当我点击可扩展单元格时,它应该在它下面显示单元格,再次点击它应该隐藏。下面有一张图片对此进行了定义。

我怎样才能实现这个功能,请指导以上。 提前致谢。

【问题讨论】:

  • 你的问题解决了吗?
  • 你检查我的答案了吗?
  • 你正在经历它..

标签: iphone ios uitableview


【解决方案1】:

Here 是可扩展 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];

            }
        }
    }
}

如下图所示

Thisthis 一种解决方案还可以帮助您了解如何管理 Expandable TableView

【讨论】:

  • 这是组表,我只要求普通表。
  • @iPhone 你检查了this 它的普通表吗
  • 我在发布问题之前看过这样的示例,这个使用部分扩展,简单地说我们只有一个部分只有单元格需要扩展。
  • @iPhone 你可以下载示例代码压缩文件here 是为表格的行扩展创建的,希望对你有帮助 祝你好运!!
  • 我得到了你的第一个教程作为正确和简单的解决方案,但它给出的问题是,当用文本完成时它是好的,但我在单元格上使用图像而不是文本和子菜单上的文本,它们是重叠的.为什么,你能帮忙吗?
【解决方案2】:

好的,我在想的是,标题、EventsOverheadFriends 将是带有 @ 的自定义 UIView 987654322@ 用于背景,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】:

    我认为你想要的东西已经被苹果给了。你可以看看苹果提供的Sample Code,标题为Table View Animations and Gestures

    【讨论】:

      【解决方案4】:

      我在下面给出了示例源代码。您可以根据您的要求自定义此示例。

      源码链接:Click here

      输出画面:

      【讨论】:

        【解决方案5】:

        JKExpandTableView 是 MIT 许可的 iOS 库,它实现了 expandable table view。请务必检查包含的示例项目。

        【讨论】:

          【解决方案6】:

          委托可以做到这一点

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

          【讨论】:

          • 你有没有为此使用过这个方法,你可以发布示例代码
          【解决方案7】:

          接受的答案是正确的,但链接已过时。要使该示例正常工作,您必须做一些额外的事情:

          从这里下载 ExpandableTableCells 项目:
          https://github.com/cocoanetics/Examples

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

          将 DTCustomColoredAccessory 文件放入 ExpandableTableCells 项目。

          这是一个工作示例,从那里编辑和移动比尝试从零开始写入更容易。

          【讨论】:

          • 我已经下载了 Download ExpandableTableCells project 并添加了下载 DTCustomColoredAccessory.h 和 .m 文件。但是我收到一个错误“错误:链接器命令失败,退出代码为 1(使用 -v 来查看调用)”你能帮忙吗?
          猜你喜欢
          • 2014-10-01
          • 2016-12-23
          • 2016-08-15
          • 2012-01-04
          • 2019-03-01
          • 1970-01-01
          • 2019-04-15
          • 2018-04-12
          • 2016-06-22
          相关资源
          最近更新 更多