UITableViewCell dynamic height does not work with size classes
Asked Answered
A

1

9

Setting estimatedRowHeight and rowHeight of UITableView makes the table view calculate proper height of each cell. That works like a charm until I use size classes. It seems like all the calculations are being done for Any/Any size class and the bigger font is applied later. As a result the height of the cell is not properly calculated and the label doesn't fit.

Here is my code:

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
    }
}

Layout looks like that: Table view layout

And the usage of size classes is: Font size classes

Now, when I open the app on an iPhone everything looks like expected. But on an iPad the cells are not properly resized. In fact they are resized to fit the text if it was font size 11pt instead of 40pt.

The question is: How can I force the calculations to be performed after the size classes were applied?

I already tried the trick with overriding trait collection as suggested in: https://stackoverflow.com/a/28514006 but it didn't work. I mean, the size class was read properly (Regular/Regular) but the font size was still 11pt.

You can download this simple project from Github: https://github.com/darecki/TableViewTest

Screenshots:

  • iPhone 4s:

enter image description here

  • iPad 2:

enter image description here

  • iPad 2 after calling tableView.reloadData():

enter image description here

Edit: Formatting, added Github link.

Ambry answered 21/9, 2015 at 12:43 Comment(9)
It is set to 0. I added screenshots to show how it does behave.Deccan
set the label's font in your custom cell's awakefromnib() based on device typeValorous
can you share your sample code?Gandzha
Sure! You can find it here: github.com/darecki/TableViewTestDeccan
Did you try to use the delegate method willDisplayCell? Maybe you could call sizeToFit() on the label there and setNeedsLayout() on the cell.Ghee
I've confirmed that this is not working on iOS 8.4 but work flawlessly on iOS 9. What did change between those?Repertory
Moreover iOS 9 says that the font size is 11 points (despite it being 40) and renders cells correctly! The same behaviour on iOS 8 (saying that font is 11 points in size) is displaying wrong cells.Repertory
Having the same issue. Label doesn't get sized properly in iOS 8Gabo
I had to turn off the option "Use Size Classes" in the controller xib that contains UITableView otherwise the cell width was always 600px and not 320px, so the text was not aligned correctly. #34712559Nonet
G
-3

Set preferred max width on the label in Interface Builder - > Size inspector. This solves the issue for me. Also make sure number of lines is 0.

Gabo answered 22/9, 2015 at 9:29 Comment(3)
This also doesn't work for me (Xcode 6.4, iOS 8.4.1). Besides, preferred max layout width cannot be applied for different size classes, so which value would you choose?Deccan
You set the width to the max the label should be. In my case its 300 (10 point padding each side of the cell. This seems to sort the problem out for me.Gabo
It really doesn't help as it doesn't match iPad's width. 300 could be fine for iPhone portrait mode. It doesn't work with size classes.Deccan

© 2022 - 2024 — McMap. All rights reserved.