I've looked at a lot of examples, but somehow they don't work. My Code Below. Objects constraints in a custom cell:
Below is the code in the class:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 200
}
//tableview ->
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 340
}

There a couple of things worth addressing:
1. Do not setup your layout in
layoutSubviews()This method will get triggered multiple times everytime there has been a change to the layout made, meaning all of these constraints will be duplicated, and will eventually cause problems. Try to set them in
initof the cell.2. Do not implement
heightForRowAtWhen implementing this function:
you are telling tableview to give the cell this exact size. According to documentation:
Remove this code!
3. Set
rowHeightto automatic size valueYou have already correctly set the
estimatedRowHeightvalue, but we are going to also need therowHeightproperty to be set toUITableView.automaticDimension.And now you should be good to go!
Bonus: Scrolling Performance improvements
Table view will benefit from any additional information on the size of the cells and you can supply that information with
estimatedHeightForRowAtif you are able to calculate a better approximate than thetableView.estimatedRowHeightvalue you have set in the initial setup.