我该如何增加UITableViewCell的高度dynamiclly

问题描述:

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

但是,我选择时需要增加UITableViewCell的高度。例如,当单元格被选中时,UITableViewCell的高度需要增加100个像素,有人知道该怎么做吗?

  1. 将选定的索引存储在控制器中。根据该指数
  2. 返回单元格的高度:

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

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

他不应该重新加载该单元格吗? – Eiko 2010-08-22 16:32:31

+0

只有在内容更改时才需要重新加载单元格。在实践中,它一定是如此的... – Vladimir 2010-08-22 16:40:15

+0

其实在那种情况下,他需要重新加载2个单元格 - 新选中的和前一个 – Vladimir 2010-08-22 16:42:24

让你委托方式

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

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