I have a TableView in a ScrollView. I set some constraint and auto layout to dynamic cell and dynamic height of table view. But i get some problems with each cell, some of each cell don’t show all of content. Any body help?
And auto layout of number in right of information label
Code of table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Code for dynamic tableview
override func updateViewConstraints() {
tableHeightConstraint.constant = tableView.contentSize.height
super.updateViewConstraints()
}
I’m stuck fews day. Many thanks.
3
Answers
add this 2 lines in viewDidLoad
check this example as well for constraints
https://github.com/iDhaval/TableCellDynamicHeight
There are few things you can do here.
layoutIfNeeded()
after setting tableView height constraintUILabel has an attribtute called "numberOfLines" and as default it is set to 1, which gives one line in a cell. We can set as many lines as We want but setting this value to 0 will allow us to adapt the number of lines in our cell to the number of lines we actually need. So add a line of code:
cell.textLabel?.numberOfLines = 0
to the func. All should looks like this: