Swift Dynamic Table Cell Height

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

Dynamically adjust the height of the tableview cell based on content - iOS Swift

Use custom cell and labels.
Set up the constrains for the UILabel. (top, left, bottom, right)
Set lines of the UILabel to 0

Add the following code in the viewDidLoad method of the ViewController:

tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableView.automaticDimension

// Delegate & data source

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableView.automaticDimension;
}

Swift 4:

// Delegate & data source

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

Swift 4.2:

// Delegate & data source

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

Cannot Set Dynamic TableView Row Height

If you want to set the dynamic tableview row height, you must use autolayout.

Please set the constraint on the UI components in the cell as shown in the image below.

Sample Image

I used UILabel as sample code. For UILabel, be sure to set Lines to 0.

Here is the sample code

class ViewController: UIViewController {

let stringArr = ["Swift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Height", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaaaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaaaa", "bbbb"]

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

tableView.dataSource = self

tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableView.automaticDimension
}
}

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as? TestCell else {
return UITableViewCell()
}

cell.label.text = stringArr[indexPath.row]

return cell
}
}
  • You need to register the cell in the tableview.

tableView.register(UINib(nibName: “YourCellNibName”, bundle: nil), forCellReuseIdentifier: “YourCellReuseIdentifier”)

Dynamically change cell height according to label text

The bottom anchor needs to be set:

cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true


Related Topics



Leave a reply



Submit