I have 2 labels on the cell. If the labels show just one line of text, then the cell looks fine. But if there is more text and if the lines on the labels are wrapped up, then the labels/text on it goes outside of the cell. Attaching image.
My current code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 68.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code to dequeue reusable cell.
self.titleLabel.text = "This is a very very long text for the title label. Testing testing"
self.subTitleLabel.text = "This is a very very long text for the sub title label. Testing testing the wrapping"
self.titleLabel.sizeToFit()
self.titleLabel.layoutIfNeeded()
self.subTitleLabel.sizeToFit()
self.subTitleLabel.layoutIfNeeded()
}
Solution I tried to fix it:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code to dequeue reusable cell.
self.titleLabel.text = "This is a very very long text for the title label. Testing testing"
self.subTitleLabel.text = "This is a very very long text for the sub title label. Testing testing the wrapping"
self.titleLabel.sizeToFit()
self.titleLabel.layoutIfNeeded()
self.subTitleLabel.sizeToFit()
self.subTitleLabel.layoutIfNeeded()
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.estimatedRowHeight = 500
self.tableView.rowHeight = UITableView.automaticDimension
}
This is how it looks with the solution. How do I update the height of the cell based on it's contents?