获取UITableView的高度

Sin*_*hia 9 iphone xcode uitableview

我创建了一个UITableview自定义UITableViewCell.The UITableViewCell包含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;
  }
Run Code Online (Sandbox Code Playgroud)

我可以通过这个获得tableViewCells的高度.如何在此之后计算/设置tableView的整体高度?

以及如何根据tableView的行/高度计算ScrollView的高度?

Par*_*shi 12

使用的contentSize.height属性UITableView.

我想你想用整个tableview设置内容大小,然后设置滚动视图大小相关的内容,UITableView并为此使用以下代码...

增加后的数据或者reloadDataUITableView只设置波纹管代码..

    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);
Run Code Online (Sandbox Code Playgroud)

更新:

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); 
Run Code Online (Sandbox Code Playgroud)