UITableViewCell动态高度不适用于大小类

时间:2015-09-21 12:43:44

标签: ios uitableview size-classes

设置estimatedRowHeight的{​​{1}}和rowHeight会使表格视图计算每个单元格的正确高度。在我使用大小类之前,它就像一个魅力。看起来所有的计算都是针对Any / Any size类进行的,稍后会应用更大的字体。结果,没有正确计算单元格的高度,并且标签不适合。

这是我的代码:

UITableView

布局看起来像这样: Table view layout

大小类的用法是: Font size classes

现在,当我在iPhone上打开应用程序时,一切看起来都像预期的那样。但是在iPad上,单元格没有正确调整大小。事实上,如果字体大小为11pt而不是40pt,它们会调整大小以适合文本。

问题是:如何在应用尺寸类后强制执行计算?

我已经尝试过覆盖特征集合的技巧,如:https://stackoverflow.com/a/28514006中所述,但它不起作用。我的意思是,大小类已正确读取(常规/常规)但字体大小仍为11pt。

您可以从Github下载这个简单的项目:https://github.com/darecki/TableViewTest

屏幕截图:

  • iPhone 4s:

enter image description here

  • iPad 2:

enter image description here

  • 调用class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() self.tableView.estimatedRowHeight = 50 self.tableView.rowHeight = UITableViewAutomaticDimension } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Reuse", forIndexPath: indexPath) as! MyTableViewCell println("Label font size: \(cell.myLabel.font)") // it prints (...)font-size: 11.00pt for all the size classes return cell } } 后的iPad 2:

enter image description here

编辑:格式化,添加了Github链接。

1 个答案:

答案 0 :(得分:-3)

在Interface Builder中设置标签上的首选最大宽度 - >尺寸检查员。 这解决了我的问题。还要确保行数为0。

相关问题