我有一个包含两个 UILabel、两个 UITableViews 和一个 UITextView 的视图。我正在尝试根据它们将显示的内容来布局这两个表。我可以很好地创建初始表格尺寸,但是当我根据表格的实际内容重置表格的高度时,表格不再显示在视图中。下面的代码片段:
-(void)viewDidLoad
{
// code to set up the first two UILabels (one of which is clinic name)
...
//
// Create the address table
//
CGRect addrRect = self.view.bounds ;
CGFloat addrStart = clinicNameRect.origin.y + (clinicNameRect.size.height)*1.5 ;
addrRect.origin.y = addrStart;
addrRect.size.height -= addrStart ; // setting it to size of rest of view which is not what I want
addressList = [[UITableView alloc] initWithFrame:addrRect style:UITableViewStyleGrouped] ;
[addressList setDataSource:self] ;
[addressList setDelegate:self] ;
[addressList setAutoresizingMask:UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth] ;
[addressList setBackgroundColor:clr] ;
[addressList setBackgroundView:nil] ; // no background
[[self view] addSubview:addressList] ; // tried moving this to after with same result
// have the system layout the table, then reset the height to actual size
[addressList layoutIfNeeded];
CGRect newFrame = [addressList frame] ;
newFrame.size.height = [addressList contentSize].height ;
// make the table's frame exactly match its size
[addressList setFrame:newFrame] ;
// go to set up next UITableView...
如果我调用 setFrame:我的表看起来很好(但在视图上占用了太多空间)。如果我留下那个电话,表格根本不会出现。