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

前端 未结 4 1548
难免孤独
难免孤独 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];
    
    }
    

提交回复
热议问题