0

I have a vertical StackView in my TableViewCell, and I set the following properties:

myTableView.estimatedRowHeight = 100.0
myTableView.rowHeight = UITableViewAutomaticDimension

But the cell height is always being set to 100 even if the collective height of the views in the cell is greater than 100. How can I make the cell size correctly given the contents in the stackview. Sometimes it might be more or less than 100.

My cell contains two stackviews ontop of eachother of equal heights with a leading and top constraint. They are set to fill/fill proportionally (alignment/distribution)

Brejuro
  • 3,421
  • 8
  • 34
  • 61
  • Can you show how your constraints are setup? – Sean Kladek Jul 06 '18 at 00:17
  • @skladek Of the tableview or the cell itself? Sorry – Brejuro Jul 06 '18 at 00:21
  • On the cell. It sounds like your cell can't determine its own height from the constraints. – Sean Kladek Jul 06 '18 at 00:29
  • @skladek Sure, updated my post – Brejuro Jul 06 '18 at 00:43
  • We need to know the constraints that you have laid out in order to let the cell determine its height. In other words, what constraints have you set between the stack view and the cell's content view? Do you have leading, trailing, top, and bottom constraints configured on the stack view so that it fills the cell? We need a little more information about your layout constraints to find an answer for you. – Nick Kohrn Jul 06 '18 at 01:53
  • @NickKohrn I only have top, leading and trailing constraints to the content view, no bottom constraint – Brejuro Jul 06 '18 at 02:16
  • 1
    @Brejuro That is likely your issue. The cell doesn't know how tall it should be because it hasn't been given enough constraints. Remember, views need to be told their position and size. You have given it a position by setting the leading, top, and trailing constraints. However, it needs to be given a size, which setting a bottom constraint will do. – Nick Kohrn Jul 06 '18 at 02:33
  • @NickKohrn But wouldn't setting a bottom constraint kind of ruin the purpose of a stack view since it needs to grow? Maybe I just don't understand stack views well enough. Also I just wanted to thank you for being so patient and pleasant with your comments, I feel like it's rare to have those qualities around here haha. – Brejuro Jul 06 '18 at 02:39
  • @Brejuro Your cells should expand and contract to fit your content just fine since you have your cells set to use `UITableViewAutomaticDimension`. If your stack view needs, let's say, a height of 1000 points to fit its content, then it knows how tall to be if you constraint it to all four sides of the cell's content view. If it only needs a height of 50 points to fit the content, then the cell will shrink to fit the content that's only 50 points tall. You'll get that behavior if you add the bottom constraint since you're telling the table view to let its cells be sized by their content. – Nick Kohrn Jul 06 '18 at 02:44
  • Couple of things you should do for self-sizing cell. Follow this article https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Shamim Hossain Jul 06 '18 at 03:15

1 Answers1

1

The cell doesn't know how tall it should be because it hasn't been given enough constraints. Remember, views need to be told their position and size. You have given it a position by setting the leading, top, and trailing constraints. However, it needs to be given a size, which setting a bottom constraint will do.

Your cells should expand and contract to fit your content just fine since you have your cells set to use UITableViewAutomaticDimension. If your stack view needs, let's say, a height of 1000 points to fit its content, then it knows how tall to be if you constrain it to all four sides of the cell's content view. If it only needs a height of 50 points to fit the content, then the cell will shrink to fit the content that's only 50 points tall. You'll get that behavior if you add the bottom constraint since you're telling the table view to let its cells be sized by their content.

Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49