Svetoslav Bramchev
Svetoslav Bramchev

Reputation: 433

UIStackView zero height although I have content in it

I have UIStackView in UITableViewCell and have four long text labels in it. In first load I show only first label with 44 height in cell and hide other three and StackView shows properly.Then when I click on tableViewCell I need to expand cell and show other three labels. I show them and reload TableView but my StackView has zero height after that and all four label have zero height too. I hit this problem only with IOS 10.1 and when I use labels with short text StackView work well.

This is my code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell  = tableView.cellForRowAtIndexPath(indexPath) as! AboutUsTableViewCell
    cell.firstLabel.hidden = isExpanded
    cell.secondLabel.hidden = isExpanded
    cell.thirdLabel.hidden = isExpanded
    isExpanded = !isExpanded
    tableView.reloadData()
}

This is my xib

This is how looks like before click

This is after click

Thank you all!

Upvotes: 1

Views: 1650

Answers (2)

Maksym Musiienko
Maksym Musiienko

Reputation: 1248

You should update table after changing isHidden properties. That's should do a trick.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell  = tableView.cellForRowAtIndexPath(indexPath) as! AboutUsTableViewCell
    cell.firstLabel.hidden = isExpanded
    cell.secondLabel.hidden = isExpanded
    cell.thirdLabel.hidden = isExpanded
    isExpanded = !isExpanded
    tableView.beginUpdates()
    tableView.endUpdates()
}

Also, check this article about using UIStackView inside UITableView cells

Upvotes: 1

Bista
Bista

Reputation: 7903

Set the Labels number of lines to 0 and add below code to the viewDidLoad method.

self.tableView.estimatedRowHeight = 44.0 //or cell height
self.tableView.rowHeight = UITableViewAutomaticDimension

Upvotes: 0

Related Questions