2013-04-07 93 views
1

我在UIView中有几个并排的UITableViews,我想让整个事情自动化。我有一个UIView在我的init()方法中:如何获得一个UITableView自动调整大小?

// I don't know how big frontBack should be, so I'd like it to autosize 
UIView *frontBack = [[UIView alloc] initWithFrame:CGRectZero]; 
frontBack.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
UITableView *table = [[UITableView alloc] 
         initWithFrame:CGRectMake(0, 0, r.size.width/2, height) style:UITableViewStyleGrouped]; 
table.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
table.dataSource = model1; 
[table reloadData]; 
[frontBack addSubview:table]; 

... (add second table similarly, except with model2) 
... 
controller.view = frontBack; 

这是行不通的。这些表格是“高度”像素高(比他们需要的小;文本被切断)。

我试图得到UITableViews调整大小的几种方法,没有运气

// contentSize is correct, but frame size does not change 
[table reloadData]; 
table.frame.size.height = table.contentSize.height; 

// contentSize is correct, but bounds does not change 
[table reloadData]; 
table.bounds.size.height = table.contentSize.height; 

// Nothing appears to change 
[table setNeedsLayout]; 
[table layoutIfNeeded]; 

// Again, no change 
[table sizeToFit]; 

我以为我失去了一些东西基本在这里,但我会很感激,如果有人能指出它是什么。

+0

你想在'UITableViews'中显示什么? TableViews是滚动视图,通常在需要时滚动它们的内容? – Tobi 2013-04-07 17:00:10

+0

另外,如果“高度”像素太小,为什么要设置它? – Tobi 2013-04-07 17:01:31

+0

你想什么时候调整大小? – ipinak 2013-04-07 21:59:08

回答

0

table.bounds.size.height = table.contentSize.height;

这是一个只读属性,您需要重新设置框架。

另外,你确定你包含的UIView没有切断表格内容吗?你也应该调整它。

+0

我试着调整它的大小,上面给出的方法,但由于UITableViews没有正确调整大小,我想我不能指望UIView正常工作。 – prewett 2013-04-08 16:23:01

+0

没有办法知道桌子的高度,你必须计算它。如果您使用的是6.0+,那么为什么不使用约束? – user352891 2013-04-08 17:06:24