在 Xcode 6 中为 iOS 8 创建应用程序时,如何将 UITableView 的 rowHeight
设置为 UITableViewAutomaticDimension
?
在WWDC 2014 Session 226 "What’s New in Table and Collection Views" ,演讲者说 UITableViewAutomaticDimension
是以编程方式创建的 TableView 的 rowHeight
的新默认值。但是,他还提到,在 session 期间,对于从 xib/storyboard 加载的 TableView ,情况并非如此。
现在 Xcode 6 GM 已经出来了,我如何在 Storyboard中设置这个值,而不必添加
self.tableView.rowHeight = UITableViewAutomaticDimension;
在我的 viewDidLoad
中?
最佳答案
Xcode 11+
从 Xcode 11 开始,Interface Builder 现在在“行高”输入附近有一个“自动”复选框。
如 release notes 中所述:
Cells in a UITableView can now self size with Auto Layout constrained views in the canvas. To opt into the behavior for existing table views, enable “Automatic” for the table view estimated item size, and “Automatic” for cell’s height in the Size inspector. (35735970)
Xcode 11 之前
在 Storyboard 中测试不同的值后,解决方案只是在 Row Height 中保留默认的 44pt
值:
这可能意味着您不能再在 Storyboard中设置 44pt 的固定高度单元格。您必须在 viewDidLoad
或其他地方以编程方式设置它。
最后,请注意,目前无法在 Storyboard 中的任何位置设置 estimatedRowHeight
,如果您不以编程方式设置它,它将默认为 0
。 (如果你知道如何做到这一点,欢迎编辑)
您可以通过在 viewDidLoad
中记录值来自行测试 44pt
行为:
NSLog(@"%f", UITableViewAutomaticDimension);
NSLog(@"%f", self.tableView.rowHeight);
将行高设置为 44
会产生:
-1.000000
-1.000000
将行高设置为另一个值(此处为 45pt
):
-1.000000
45.00000
关于ios - 在 Storyboard中将 UITableView 的 rowHeight 设置为 UITableViewAutomaticDimension?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888126/