Uitableviewcell Height Issue

UITableViewCell Height Issue

Well, you can't achieve the dynamic height of cell with UITableViewAutomaticDimension on the basis of image's constraints.

BUT image's height and width can help you in this. There is a plenty of help regarding this issue is available in following answer:

Dynamic UIImageView Size Within UITableView

UITableView Custom Cell Height Not Working

You haven't implemented the UITableViewDelegate

In your setupTable() add the line

tableView.delegate = self

Then change your extension from

extension ViewController: UITableViewDataSource {

to extension ViewController: UITableViewDataSource, UITableViewDelegate {

How to use dynamic height of UITableViewCell in auto-layout and move other views to up when bottom view is hidden?

At first, make the height of UITableViewCell to UITableView.automaticDimension

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}

Embed all of your questionLabels in a UIStackView (vertical) excluding bottomLabel. Set AutoLayoutConstraint between UIStackView and bottomLabel.

Sample Image

Set the numberOfLines property of UILabels to 0(zero).

Sample Image

Set the Distribution of the UIStackView as Fill

Sample Image

Then, in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method hide the labels. And it will automatically handle the spaces between UILabels

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell

cell.questionLabel1.text = labelOneText[indexPath.row]
cell.questionLabel2.text = labelTwoText[indexPath.row]
cell.questionLabel3.text = labelThreeText[indexPath.row]

if labelOneText[indexPath.row] == "" {
cell.questionLabel1.isHidden = true
}

if labelTwoText[indexPath.row] == "" {
cell.questionLabel2.isHidden = true
}

if labelThreeText[indexPath.row] == "" {
cell.questionLabel3.isHidden = true
}

return cell
}

Final Output:

Sample Image

UITableViewCell height auto layout not working on iOS 10

This configuration from your viewDidLoad:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

...works only on iOS 11. It is an iOS 11 innovation and does not apply to earlier systems.

For iOS 10, you must supply an actual estimatedRowHeight value (such as 60) to turn on automatic variable row height calculation.



Related Topics



Leave a reply



Submit