How can I increase the height of UITableViewCell dynamiclly

前端 未结 2 1413
遇见更好的自我
遇见更好的自我 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];      
      }
      
    0 讨论(0)
  • 2021-01-07 04:30

    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 讨论(0)
提交回复
热议问题