【问题标题】:How can I increase the height of UITableViewCell dynamiclly如何动态增加 UITableViewCell 的高度
【发布时间】:2010-08-22 16:20:01
【问题描述】:

我有一个包含 UITableViewCells 列表的 UITableView。我在方法(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath中设置了UITableViewCells的高度。

但是,我需要在选中UITableViewCell 时增加它的高度。比如UITableViewCell的高度在选中单元格的时候需要增加100像素,有人知道怎么做吗?

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:
    1. 将选定的索引存储在控制器中。
    2. 根据该索引返回单元格的高度:

      CGFloat height = 30.0f;
      ...
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
          return indexPath.row == selectedIndex ? height+100 : height;
      }
      
    3. didSelectRowAtIndexPath 方法中刷新tableview 几何(beginUpdatesendUpdates 之间的空块可以解决问题):

      - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
          index = indexPath.row;
          [tableView beginUpdates];
          [tableView endUpdates];      
      }
      

    【讨论】:

    • 他不应该重新加载那个单元格吗?
    • 只有在内容发生变化时才需要重新加载单元格。在实践中它确实必须如此......
    • 实际上在这种情况下他需要重新加载 2 个单元格 - 新选择的单元格和上一个单元格
    • 不错。我也有同样的困惑,首先我尝试使用 [tableview reloaddata] 方法来刷新几何图形,这需要几秒钟。 beginUpdates/endUpdates 是更好和正确的方法。
    【解决方案2】:

    制作你的委托方法

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    返回(新的)正确高度并使表格视图重新加载给定路径。

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2011-03-05
      • 1970-01-01
      相关资源
      最近更新 更多