I have tried to hard code the row height in the table view cell. After running the program it looks like only one line. I suspect it is because of the height of the row
Add this code in your UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
Set row height as well as estimated row height as Automatic in viewDidLoad()
as
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
In heightForRowAt:
set height as UITableViewAutomaticDimension
as
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Note: Set number of lines of label long restaurant name
as 0.
If you want the cells to occupy space according to its content, you need to return UITableViewAutomaticDimension in the table view delegate methods heightForRowAt: and estimatedHeightForRowAt:
yourTableView.rowHeight = UITableView.automaticDimension and
yourTableView.estimatedRowHeight = UITableView.automaticDimension
return UITableView.automaticDimension
in tableView(_:heightForRowAt:)
delegate method
Set all constraints . Bottom constraint is important to increase table view cell height .
Your all set to go
Following this guide which worked for me, you have to add a bunch of constraints and then the rowheight is calculated automatically.
In short, what I did: (adding and modifying constraints are the two rightmost buttons bottom right of the storyboard view)I at first selected all views in the prototype cell, deleted all constraints (there is a nice option on the rightmost button), then added all missing constraints (another nice option there) and last I chose the second button from the right and clicked all the red lines. You can do this while having all views selected at the same time. And then it worked.
Ensure following steps to make, auto dimension effective for cell/row height layout. (Here is sample code for UILabel and how to set content specific height for table cell.)
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeightheightForRowAt
and return a value UITableViewAutomaticDimension
to it)-
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
For label instance in UITableviewCell