xcode 获取 UITableView 的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14119916/
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
Get UITableView's height
提问by Sindhia
I have created a UITableview
with custom UITableViewCell
.The UITableViewCell
contains UILabels
and I have calculated the height of each cell based on the cells height.But how can I get the tableview height?Because based on the tableView's height I need to calculate
the height of scrollView
I have created a UITableView
like this when a button is clicked:
我创建了一个UITableview
自定义UITableViewCell
.TheUITableViewCell
包含UILabels
我已经计算基于细胞height.But我怎么能得到的tableview高度的每个单元的高度呢?因为基础上的tableView的高度我需要计算滚动视图我的高度,创造了一个UITableView
这样当按钮被点击:
-(IBAction)btnClicked:(id)sender
{
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[arr5 count]*205) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
float fscrview = 150 + self.tableView.frame.size.height + 20;
testscroll.contentSize=CGSizeMake(320, fscrview);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *city1 = city.text;
UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds = [city1 sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
//similarly calculated for all
return (CGFloat) cell.bounds.size.height + bounds.height+bounds1.height+bounds2.height+bounds3.height+bounds4.height+bounds5.height;
}
I am able to get tableViewCells height through this.How do I calculate/set the overall tableView's height after this?
我可以通过这个获得 tableViewCells 的高度。在此之后如何计算/设置整个 tableView 的高度?
And how to calculate ScrollView's height based on tableView's row/height?
以及如何根据 tableView 的行/高度计算 ScrollView 的高度?
回答by Paras Joshi
Use contentSize.height
property of UITableView
.
使用 的contentSize.height
属性UITableView
。
I think you want to set the whole tableview with content size and then set the scrollview size related content of UITableView
and for this use bellow code...
我想你想用内容大小设置整个tableview,然后设置滚动视图大小相关的内容,UITableView
为此使用波纹管代码......
After add data or reloadData
in UITableView
just set bellow code..
之后添加数据或reloadData
在UITableView
只设置波纹管代码..
yourTableView.frame = CGRectMake(yourTableView.frame.origin.x,yourTableView.frame.origin.y, yourTableView.frame.size.width, yourTableView.contentSize.height);
float ftbl = yourTableView.frame.origin.y + yourTableView.contentSize.height + 15;
yourScrollView.contentSize=CGSizeMake(320, ftbl);
UPDATE:
更新:
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[arr5 count]*205)style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
[testscroll addSubview:self.tableView]; /// add this tableview in scrollview not in self.view
[self.tableView reloadData];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 15;
testscroll.contentSize=CGSizeMake(320, ftbl);
回答by Irshad Mansuri
Very simplest way to get your UITableView height
获取 UITableView 高度的最简单方法
- (CGFloat)tableViewHeight
{
[tblData layoutIfNeeded];
return [YOUR_TABLE_NAME contentSize].height;
}
回答by Anderson Tenorio Tagata
on iOS 8+ this worked to get the table view Height
在 iOS 8+ 上,这有助于获得表格视图高度
CGFloat tableViewHeight = tableView.bounds.size.height;
回答by DontTurnAround
Try this: tableView.backgroundView.bounds.size.height
Should work
试试这个:tableView.backgroundView.bounds.size.height
应该工作
Logic:
逻辑:
UITableView
has a property,backgroundView
which is "A table view's background view is automatically resized to match the size of the table view."backgroundView
is aUIView
which has the propertybounds
which is aCGRect
that "defines the size and position of the view."CGRect
has asize
property,size
has aheight
property
UITableView
有一个属性,backgroundView
即“表视图的背景视图会自动调整大小以匹配表视图的大小。”backgroundView
是UIView
其具有这样的特性bounds
,其是CGRect
该“定义的尺寸和视图的位置”。CGRect
有一个size
属性,size
有height
属性
QED
QED
回答by Vlad E. Borovtsov
You also can use KVO to observer tableview's contentSize property and adjust what you need in other views.
您还可以使用 KVO 来观察 tableview 的 contentSize 属性并在其他视图中调整您需要的内容。
Put it somewhere, e.g. in viewDidLoad:
把它放在某个地方,例如在 viewDidLoad 中:
[self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
Then implement observer:
然后实现观察者:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (object == self.tableView) {
self.scrollViewHeightConstraint.constant = self.tableView.contentSize.height;
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}