开发者

How can I increase the height of UITableViewCell dynamiclly

开发者 https://www.devze.com 2023-01-13 02:43 出处:网络
I have an UITableView which includes a list of UITableViewCells. And I set the heights of UITableViewCells in method (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)

I have an UITableView which includes a list of UITableViewCells. And I set the heights of UITableViewCells in method (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

However, I need to increase the height of UITableViewCell when it is selected. For example开发者_开发知识库, the height of UITableViewCell needs to increase 100 pixels when the cell is selected, does anyone know how to do it?


  1. Store selected index in your controller.
  2. Return cell's height depending on that index:

    CGFloat height = 30.0f;
    ...
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return indexPath.row == selectedIndex ? height+100 : height;
    }
    
  3. Refresh tableview geometry in didSelectRowAtIndexPath method (empty block between beginUpdates and endUpdates does the trick):

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


Make your delegate method

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

return the (new) correct height and make the table view reload the given path.

0

精彩评论

暂无评论...
验证码 换一张
取 消