【问题标题】:UITableViewAutomaticDimension not working properly with Subtitle UITableViewCellsUITableViewAutomaticDimension 无法与字幕 UITableViewCells 一起正常工作
【发布时间】:2015-10-20 12:07:13
【问题描述】:

我正在创建 2 个部分 UITableView。第一部分使用UITableViewCellsDefault 样式,第二部分使用Subtitle 样式。两个单元格都可能有一个多行文本标签。

我已将表格的 rowHeight 设置为 UITableViewAutomaticDimension

从下面的屏幕截图中可以看出,UITableViewAutomaticDimension 在使用Subtitle 样式时仅受到部分尊重:高度似乎适合textLabel 的高度,但不适合detailLabel 的高度。

如何使Subtitle 单元格获得正确的高度?

一些代码:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.estimatedRowHeight = 80
    tableView.rowHeight = UITableViewAutomaticDimension

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section === 1 {
        var cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "subtitleCell")
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = "A Long text..."
        cell.detailTextLabel?.numberOfLines = 0
        cell.detailTextLabel?.text =  "A Long text..."
        return cell
    }
    else {
        var cell = tableView.dequeueReusableCellWithIdentifier("defaultCell") as! UITableViewCell

        cell.textLabel!.text = "A long text..."
        cell.textLabel?.numberOfLines = 0
        return cell
    }

}

我尝试实现UITableViewCell 的子类,但我在自动布局和章节标题方面存在很多问题(说来话长),所以我想知道是否可以用更简单的方式解决这个问题。

【问题讨论】:

  • 你能解决这个问题吗?

标签: ios uitableview ios8 uikit


【解决方案1】:

刚刚试了Xcode 9iPhone 8 simulator,效果很好。

代码在这里https://github.com/williamhqs/testSubtitleCellLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2017-12-14
    • 2020-08-19
    • 2013-02-06
    相关资源
    最近更新 更多