ios - 在 Storyboard中将 UITableView 的 rowHeight 设置为 UITableViewAutomaticDimension?

标签 ios objective-c uitableview storyboard autolayout

在 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 Interface Builder


Xcode 11 之前

在 Storyboard 中测试不同的值后,解决方案只是在 Row Height 中保留默认的 44pt 值:

Xcode 6 Interface Builder

这可能意味着您不能再在 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/

相关文章:

ios - 是否可以在 Objective-C 中为静态变量设置 KVO 通知?

IOS/ Storyboard : Create Outlet Property for Text of Tabbar Item on Toolbar

ios - 调试 iPhone Chrome

objective-c - Objective-C Controller 作为带有协议(protocol)的模型委托(delegate)?

ios - Objective c 在转换为 UIImage 时丢失了 CIImage 内存

ios - 有适用于 iOS 的 ascii85/base85 编码器/解码器吗?

ios - Obj-C-显示对应数组中每个单元格的正确数据

ios - 如何在 Storyboard 的 UITableView 中显示来自 sqlite 的数据

ios - 使用 Realm 尝试删除一个对象会引发异常 ('Can only add an object to a Realm in a write transaction...' )

iphone - 在 UITableview 中选择多行