I have a very simple UITableViewController
, where I want to set a text on each textLabel
of the UITableViewCell
s.
The height should be calculated by autolayout. Therefore, in my viewDidLoad
set following values to the tableView
properties:
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
This is my cellForRow
method, where I initialize the default UITableViewCell
and set the text to its label.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello"
return cell
}
My problem is, that the cell won't grow according to the size of the label. This issue only appears on iOS 10. On iOS 11 everything gets layed out as it should.
Please check following screenshots:
EDIT: 24th of May 2018
Having the textLabel
only, it will work if there is an actual value on estimatedRowHeight
.
But having a cell with style .subtitle
, and setting a value to the descriptionLabel
, the layouting won't work. Same issue: iOS 10 does not work. iOS 11 works (cell resizes according to both labels).
This configuration from your viewDidLoad
:
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
...works only on iOS 11. It is an iOS 11 innovation and does not apply to earlier systems.
For iOS 10, you must supply an actual estimatedRowHeight
value (such as 60
) to turn on automatic variable row height calculation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With