ios 如何获取 UITableView 中特定行的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18206448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the height of a specific row in my UITableView
提问by aneuryzm
In my UITableView
i've set different heights for different rows using the delegate method: tableView:heightForRowAtIndexPath:
在我中,UITableView
我使用委托方法为不同的行设置了不同的高度:tableView:heightForRowAtIndexPath:
Now given a NSIndexPath
, I would like to get the height I've previously assigned for a specific row.
现在给出一个NSIndexPath
,我想获得我之前为特定行分配的高度。
回答by Toseef Khilji
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
使用frame.size.height
您可以获得特定行的高度
Swift 3
斯威夫特 3
let frame = tableView.rectForRow(at: indexPath)
print(frame.size.height)
回答by abhishekkharwar
Simplly Use
简单使用
tableView:heightForRowAtIndexPath
表视图: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 :)
希望这对你有帮助:)
回答by iphonic
-(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
调用上面的函数tableView:heightForRowAtIndexPath:
作为
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
并且您可以在特定单元格内的按钮的说按钮单击操作上使用相同的功能,具有 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];
}
回答by Vineesh TP
By using below code you can get the cell height from tableview
通过使用下面的代码,您可以获得单元格高度 tableview
let height = super.tableView(tableView, heightForRowAt: indexPath)