How can I get the height of a specific row in my UITableView

前端 未结 4 1547
难免孤独
难免孤独 2021-02-04 00:29

In my UITableView i\'ve set different heights for different rows using the delegate method: tableView:heightForRowAtIndexPath:

Now given a

相关标签:
4条回答
  • 2021-02-04 00:30
    -(CGFloat)getHeightAtIndexPath:(NSIndexPath *)indexPath{
       if(indexPath.row == 0)
           return 20.0;
       else if(indexPath.row == 4)
           return 40.0;
    
      return 50.0; //Default
    
    }
    

    Call the above function in tableView:heightForRowAtIndexPath: as

    return [self getHeightAtIndexPath:indexPath];
    

    And you can use the same function on say button click action of a button inside a specific cell, with having button.tag=indexPath.row

    -(IBAction)btnClick:(id)sender{
       UIButton *btn=(UIButton *)sender;
    
       NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btn.tag inSection:0];
    
       CGFloat height=[self getHeightAtIndexPath:indexPath];
    
    }
    
    0 讨论(0)
  • 2021-02-04 00:44

    Simplly Use

    tableView:heightForRowAtIndexPath

    method

    int row = 1;
    
    int section = 0;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    
    float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    

    Hope this wil help for you :)

    0 讨论(0)
  • 2021-02-04 00:47

    You can use this

    CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
    NSLog(@"row height : %f", frame.size.height);
    

    Using frame.size.height you can get height of particular row

    Swift 3

    let frame = tableView.rectForRow(at: indexPath)
    print(frame.size.height)
    
    0 讨论(0)
  • 2021-02-04 00:49

    By using below code you can get the cell height from tableview

     let height = super.tableView(tableView, heightForRowAt: indexPath)
    
    0 讨论(0)
提交回复
热议问题