New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the conten
Swift 4+
working(Tested 100%)
If you need both section as well row with dynamic height based on content then you can use below code:
On viewDidLoad() write this lines:
self.globalTableView.estimatedRowHeight = 20
self.globalTableView.rowHeight = UITableView.automaticDimension
self.globalTableView.sectionHeaderHeight = UITableView.automaticDimension
self.globalTableView.estimatedSectionHeaderHeight = 25;
Now we have set row height and section height by using UITableView Delegate methods:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
This can be accomplished by setting (or returning) the estimatedSectionHeaderHeight
on your table view.
If your section header is overlapping your cells after setting estimatedSectionHeaderHeight
, make sure that you're using an estimatedRowHeight
as well.
(I'm adding this answer because the second paragraph contains an answer to an issue that can be found after reading through all of the comments which some might miss.)