I am currently messing around with fast and dynamic table heights. I developed a simple program for iOS8.1 on xcode6.1: https://github.com/ArtworkAD/DynamicCellTest
Thus, to achieve a cell height that stretches with the contents of the cell, I do the following:
- labels are set to 0 in the schedule lines
- set font labels to system
- set limits for the label in the cell
- add
self.tableView.rowHeight = UITableViewAutomaticDimension - do not override
heightForRowAtIndex method
Minimum code required:
class MyTableViewController: UITableViewController { var entries:Array<String> = [String]() override func viewDidLoad() { super.viewDidLoad() //create dummy content var i = 0 while i < 10 { entries.append("\(i) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor") entries.append("\(i+1) Lorem ipsum dolor sit amet") i = i + 2; } 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 { 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 } }
The left image shows the result of the above code. The height of the cell grows with the content of the label, everything is beautiful. However, when you click on the disclosure indicator on the detailed view and return again, you get the correct image. Why is this happening?

A bad solution to this problem is to override these methods:
override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) self.tableView.reloadData() } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.tableView.reloadData() } override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) self.tableView.reloadData() }
So the problem is solved, but does this solution seem to be wrong? A side effect of this is that when you call self.tableView.reloadData() port of the table view goes to the first cell, which does not look beautiful.
Does anyone have an idea what I'm doing wrong? Feel free to clone my repo https://github.com/ArtworkAD/DynamicCellTest and test it.
source share