获取UITableView的高度 [英] Get UITableView's height
问题描述
我使用自定义 UITableViewCell
创建了 UITableview
。 UITableViewCell
包含 UILabels
我根据单元格高度计算了每个单元格的高度。但是我怎样才能获得tableview高度?因为我需要基于tableView的高度计算
scrollView
的高度当点击一个按钮时我创建了一个 UITableView
:
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:
-(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;
}
我能通过这个得到tableViewCells的高度。我如何计算/设置此后整体tableView的高度?
I am able to get tableViewCells height through this.How do I calculate/set the overall tableView's height after this?
如何根据tableView的行/高度计算ScrollView的高度?
And how to calculate ScrollView's height based on tableView's row/height?
推荐答案
使用 contentSize.height
UITableView
的属性。
我想你想用内容大小设置整个tableview,然后设置 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...
在 UITableView
reloadData 之后>只需设置以下代码..
After add data or reloadData
in UITableView
just set bellow code..
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);
更新:
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);
这篇关于获取UITableView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!