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.

Community
  • 1
  • 1
  • It is set to 0. I added screenshots to show how it does behave. – Darek Cieśla Sep 21 '15 at 14:25
  • set the label's font in your custom cell's awakefromnib() based on device type – dopcn Sep 21 '15 at 15:39
  • can you share your sample code? – Johnykutty Sep 22 '15 at 06:23
  • Sure! You can find it here: https://github.com/darecki/TableViewTest – Darek Cieśla Sep 22 '15 at 06:43
  • Did you try to use the delegate method willDisplayCell? Maybe you could call sizeToFit() on the label there and setNeedsLayout() on the cell. – Ted Huinink Sep 22 '15 at 07:02
  • 3
    I've confirmed that this is not working on iOS 8.4 but work flawlessly on iOS 9. What did change between those? – Ertai Sep 22 '15 at 07:18
  • 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. – Ertai Sep 22 '15 at 07:23
  • Having the same issue. Label doesn't get sized properly in iOS 8 – Robert J. Clegg Sep 22 '15 at 08:49
  • 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. https://stackoverflow.com/questions/34712059/text-in-dtattributedtextcontentview-is-not-aligned-correctly – Vladimír Slavík Jan 11 '16 at 13:45

1 Answers1

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

Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99
  • 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? – Darek Cieśla Sep 22 '15 at 09:53
  • 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. – Robert J. Clegg Sep 22 '15 at 09:59
  • 2
    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. – Darek Cieśla Sep 22 '15 at 11:01