6

I'm creating a 2 sections UITableView. The first section uses UITableViewCells with the Default style, the second uses Subtitle style. Both cells may have a multiline text label.

I've set the table's rowHeight to UITableViewAutomaticDimension.

As you can see from the screenshot below, UITableViewAutomaticDimension is respected only partially when using the Subtitle style: the height seems to fit the textLabel's height but not the detailLabel's one.

How can i make a Subtitle cell getting the right height?

Some code:

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
    }

}

I've tried to implement a subclass of UITableViewCell but I many problems with autolayout and section titles (a long story), so I wonder if I could fix the issue in a simpler way.

gpbl
  • 4,721
  • 5
  • 31
  • 45

1 Answers1

0

Just tried on Xcode 9 and iPhone 8 simulator, works very well.

code is here https://github.com/williamhqs/testSubtitleCellLayout

enter image description here

William Hu
  • 15,423
  • 11
  • 100
  • 121