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

Dar*_*śla 9 uitableview ios size-classes

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

这是我的代码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

布局看起来像这样: 表视图布局

而size类的用法是: 字体大小类

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

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

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

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

截图:

  • iPhone 4s:

在此输入图像描述

  • iPad 2:

在此输入图像描述

  • iPad 2致电后tableView.reloadData():

在此输入图像描述

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

Rob*_*egg -3

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

  • 它确实没有帮助,因为它与 iPad 的宽度不匹配。300 对于 iPhone 肖像模式来说就足够了。它不适用于尺寸类别。 (2认同)