Faraaz
Faraaz

Reputation: 137

How to change layout of a UITableViewCell with a UIStackView

I have a custom UITableViewCell with a vertical UIStackView. The stack view in-turn has 2 UILabels, one of it is hidden by default. How to change the height of the cell to show both labels in the stack view properly when the second label is unhidden. On a button click when i show the second label, it tries to show both label with the same height overlapping each other, however when i scroll the view up and down, it re-renders it ok.

I tried to call SizeToFit and LayoutIfNeeded with no help. How do i change the height of the row/cell to show both labels in the stackview to look proper?

Cell with a stackview and second label hidden Cell with a stackview and second label unhidden

Upvotes: 0

Views: 984

Answers (3)

Stas Telnov
Stas Telnov

Reputation: 305

You can call

tableView.beginUpdates()
tableVIew.endUpdates()

After tap on button, and your cell change height. It solution is compatible with animation.

Upvotes: 1

andrija
andrija

Reputation: 342

Try reloading that row with reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) after you click the button that shows both labels.

Upvotes: 0

Hai Dang Nguyen
Hai Dang Nguyen

Reputation: 26

On the button click, the label 2nd is showed, you should reload the table view, or reload exactly the cell, the height of cell will change and the cell will be re-rendered

Upvotes: 0

Related Questions