如何动态增加UITableViewCell的高度

时间:2010-08-22 16:20:01

标签: iphone cocoa-touch uitableview

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

但是,我需要在选择时增加UITableViewCell的高度。例如,当选择单元格时,UITableViewCell的高度需要增加100像素,是否有人知道如何操作?

2 个答案:

答案 0 :(得分:11)

  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];      
    }
    

答案 1 :(得分:0)

制作代表方法

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

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

相关问题