簡體   English   中英

如何獲取 UITableView 中特定行的高度

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

在我的UITableView我使用委托方法為不同的行設置了不同的高度: tableView:heightForRowAtIndexPath:

現在給定一個NSIndexPath ,我想獲得我之前為特定行分配的高度。

你可以用這個

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

使用frame.size.height您可以獲得特定行的高度

斯威夫特 3

let frame = tableView.rectForRow(at: indexPath)
print(frame.size.height)

簡單使用

表視圖:heightForRowAtIndexPath

方法

int row = 1;

int section = 0;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];

希望這對你有幫助:)

-(CGFloat)getHeightAtIndexPath:(NSIndexPath *)indexPath{
   if(indexPath.row == 0)
       return 20.0;
   else if(indexPath.row == 4)
       return 40.0;

  return 50.0; //Default

}

tableView:heightForRowAtIndexPath:調用上面的函數tableView:heightForRowAtIndexPath:

return [self getHeightAtIndexPath:indexPath];

您可以在特定單元格內的按鈕的按鈕單擊操作上使用相同的功能,並具有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];

}

通過使用下面的代碼,您可以從tableview獲取單元格高度

 let height = super.tableView(tableView, heightForRowAt: indexPath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM