• UITableView:可展开的 UITableView


    针对 TableView,有些时候需要在点击 cell 时,展开这行 cell,显现出更多的选项或者全部内容等。

    比较容易想到的处理方案就是利用 section,在未选择之前,每一行都是一个 section,待展开的内容都在这个 section 的 row 里面。在点击事件里,reload 当前选中的 section,在 numberOfRows 方法中,返回大于 0 的数量。

    这里谈谈另外一种很简单的方法,直接在 TableViewCell 上面做文章。

    1.点击 cell 时,在 didSelectRowAtIndexPath 方法中加两个方法

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView beginUpdates];
    //    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //    cell.textLabel.text = [NSString stringWithFormat:@"Did Select %@", indexPath];
        [tableView endUpdates];
    }
    

     2.如果需要变化这行 row 的高度,在 heightForRow 方法中,加入如下处理

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
            return 88;
        }
        return 44;
    }
    

     3.在 TableViewCell 的 setSelected:(BOOL)selected animated:(BOOL)animated 方法中,为选中和未选中分别做下处理即可。

    #重要

    beginUpdates 和 endUpdates 两个方法之间虽然什么也没有写,但是,这与 reloadData 是不同的,前者可以保持住 cell 的 selected 属性,而后者则把 selected 属性重置了。

  • 相关阅读:
    迭代器、生成器
    函数(函数基础、装饰器、递归、匿名函数)
    文件处理
    python对象、引用
    字符编码
    流程控制if、while、for
    编程与编程语言
    Java源码阅读(五)—— AbstractQueuedSynchronizer
    Java并发编程(二) —— volatile
    Java源码阅读(七)—— ReentrantReadWriteLock
  • 原文地址:https://www.cnblogs.com/ihojin/p/expansion-tableview.html
Copyright © 2020-2023  润新知