如何动态增加UITableViewCell的高度

Mic*_*l C 2 iphone cocoa-touch uitableview

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

但是,我需要增加UITableViewCell选择时的高度.例如,UITableViewCell选择单元格时需要增加100像素的高度,有谁知道怎么做?

Vla*_*mir 11

  1. 将所选索引存储在控制器中.
  2. 返回单元格的高度取决于该索引:

    CGFloat height = 30.0f;
    ...
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return indexPath.row == selectedIndex ? height+100 : height;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 刷新didSelectRowAtIndexPath方法中的tableview几何体(空格块之间beginUpdatesendUpdates执行操作):

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