Dynamic UITableCellView height

I followed this tutorial to enable dynamic cell height:

So I added

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

in viewDidLoad, set labels to 0 Lines and font set to System. The result is that the size of the cell is now dynamic and content is shown correctly but NOT when the table is first displayed. Well some cells are shown correctly but others have a height of 44. When I scroll the table the height seem to be corrected. In the tutorial they describe it like this:

When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears

I tried with

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 64
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.tableView.reloadData()
}

but it isn't working. Any ideas how to fix this? I am on xcode 6.1 and iOS8.1 and I am using swift.

I created a simple test project on github:


ANSWERS:


I downloaded the project you created on GitHub and made a few changes and got it working.

The code in the ViewController looks like this...

import UIKit

class MyTableViewController: UITableViewController {

    var entries:Array<String> = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var i = 0
        while i < 20 {
            entries.append("\(i) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor")
            i++;
        }

        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
        let cell = self.tableView.dequeueReusableCellWithIdentifier("basic_cell", forIndexPath: indexPath) as UITableViewCell

        var label = cell.viewWithTag(13)

        if let unwrappedLabel = label as? UILabel {
            unwrappedLabel.text = self.entries[indexPath.row]
        }

        return cell
    }
}

And the Storyboard looks like this...

enter image description here

Notice the AutoLayout constraints going from top to bottom of the cell and I set the number of lines to 0.

Then when running the app...

enter image description here


I think is ok to reload the table in willDisplayCellMethod inside a dispatch_once block. It is an objective C syntax. You can work out in swift.

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        [self.tableView reloadData];
    });
}


 MORE:


 ? Dynamic UITableCellView height
 ? Dynamic UITableCellView height
 ? Dynamic UITableCellView height
 ? Dynamic UITableCellView height with cell indicator
 ? Dynamic UITableCellView height with cell indicator
 ? UITableView dynamic cell height breaks on transition or rotation
 ? UITableViewCell dynamic height breaks after insert row
 ? iOS8 implementing heightForRowAtIndexPath with dynamic heights
 ? Dynamic cell height issue with UITableViewCell autolayout jerk while scrolling
 ? UITableView to increase height dynamically with Scrolling Disabled?