How can I increase the height of UITableViewCell dynamiclly

前端 未结 2 1436
遇见更好的自我
遇见更好的自我 2021-01-07 04:10

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

2条回答
  •  清歌不尽
    2021-01-07 04:27

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

提交回复
热议问题