【发布时间】:2015-12-18 04:09:49
【问题描述】:
设置estimatedRowHeight 和rowHeight 的UITableView 使表格视图计算每个单元格的正确高度。在我使用尺寸等级之前,这就像一种魅力。似乎所有的计算都是针对 Any/Any 大小类进行的,并且稍后会应用更大的字体。因此,单元格的高度计算不正确,标签不适合。
这是我的代码:
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
}
}
现在,当我在 iPhone 上打开应用程序时,一切看起来都像预期的那样。但在 iPad 上,单元格的大小未正确调整。事实上,如果字体大小为 11pt 而不是 40pt,它们会调整大小以适应文本。
问题是:如何在应用尺寸等级后强制执行计算?
我已经按照https://stackoverflow.com/a/28514006 中的建议尝试了覆盖特征集合的技巧,但它没有用。我的意思是,大小类被正确读取(Regular/Regular),但字体大小仍然是 11pt。
你可以从 Github 下载这个简单的项目:https://github.com/darecki/TableViewTest
截图:
- iPhone 4s:
- iPad 2:
- 拨打
tableView.reloadData()后的iPad 2:
编辑:格式化,添加 Github 链接。
【问题讨论】:
-
它设置为 0。我添加了屏幕截图以显示它的行为方式。
-
根据设备类型在自定义单元格的 awakefromnib() 中设置标签字体
-
你能分享你的示例代码吗?
-
当然!你可以在这里找到它:github.com/darecki/TableViewTest
-
我已经确认这不适用于 iOS 8.4,但在 iOS 9 上可以完美运行。它们之间有什么变化?
标签: ios uitableview size-classes