UITableViewAutomaticDimension does not work properly with Subtitle UITableViewCells

I am creating 2 sections of a UITableView . The first section uses UITableViewCells with the Default style, the second uses the Subtitle style. Both cells can have a multi-line text label.

I set the rowHeight table to UITableViewAutomaticDimension .

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

How can I make a Subtitle cell get the correct height?

Some codes:

 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 tried to implement a subclass of UITableViewCell , but I have a lot of problems with autorun and section headers (long story), so I wonder if I can solve the problem in a simpler way.

+6
source share
1 answer

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

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

enter image description here

0
source

All Articles