zoukankan      html  css  js  c++  java
  • 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 属性重置了。

  • 相关阅读:
    OC与JS交互之WKWebView
    iOS下JS与OC互相调用(三)--MessageHandler
    html base64 img 图片显示
    Vue中img的src属性绑定与static文件夹
    XML 树结构
    XML 用途
    XML 简介
    JS Window对象
    JS Math对象
    JS 字符串操作
  • 原文地址:https://www.cnblogs.com/ihojin/p/expansion-tableview.html
Copyright © 2011-2022 走看看