4

I have an app with several UITableView controllers. Running on iOS 8.x, the height of all the cells in each table would resize to fit the content of the cell (all contain just a UILabel with plain text). Now when running on iOS 9, every cell on every table is only one line tall. This is with both dynamic and static tables. I've scoured the UIKit diffs document and done extensive searching, but I can't find the right combination of things to get anything other than a height of one line in all the cells in all the tables.

ChiliOcean
  • 61
  • 1
  • 6
  • 2
    I was able to fix the table with dynamic cells by adding to this to viewDidLoad `self.tableView.estimatedRowHeight = 100.0; self.tableView.rowHeight = UITableViewAutomaticDimension;` But I still haven't found a way to fix the tables with static cells. – ChiliOcean Sep 12 '15 at 04:32
  • For some reason, setting the numberOfLines = 0, in the storyboard did not work. Setting it in cellForRowAtIndexPath worked in getting the proper heights. In addition calling the delegate estimatedRowHeight helped in the margins being more visually appealing. – runios Jan 18 '16 at 05:16

3 Answers3

11

I've run into a similar case. The trick seems to be to implement the dynamic sizing via UITableView's delegate methods explicitly. Even though settings automatic in storyboards is supposed to work - it doesn't. The solution is to provide UITableViewAutomaticDimension explicitly via the delegate method and then provide estimated cell sizes as you normally would:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    /* Return an estimated height or calculate 
     * estimated height dynamically on information 
     * that makes sense in your case.
     */
    return 200.0f;
}

If anyone knows precisely why this is necessary in iOS 9 and how it differs from iOS 8, I'd love to hear it.

dbart
  • 5,468
  • 2
  • 23
  • 19
1

We can use "UITableViewAutomaticDimension" in iOS 8 to implement the dynamic sizing explicitly by using following two properties:-

tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension

Then add following code in "cellForRowAtIndexPath" method before return cell.

cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

And for iOS 9 we can add tableView's delegate method:-

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
Zalak Patel
  • 1,937
  • 3
  • 26
  • 44
0

There is a UIStackView issue on iOS 9-10 when using layoutMargins

extension UIStackView {
    open override func didMoveToSuperview() {   // a workaround for stackview issues on iOS 9-10
        super.didMoveToSuperview()
        if #available(iOS 11.0, *) { } else {
            layoutMargins = self.layoutMargins
        }
    }
}