-1

I have a UITableViewCell whose height needs atleast 106. I need to set the contentView height to increase if in case the label inside the cell surpasses the height 106.

Here is what i did.

  • Set up a containerView to fit all edges.
  • Set the height anchor of containerView > 106
  • Set the label with top and leading.

What else do i need to do so that the cell gets it's height?

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • On a second thought, if you are using automatic height for cell, cells will take implicit size, so if you add UIComponents to cell which can effectively provide the height for cell, then obviously the perfect height to accomamdate all UI components will be used :) else you can always calculate possible height in heightForRowAtIndexPath check if its less than 106 if yes return 106 else return whatever value you get. Auto layout constraint on cell's contentView does not make much sens – Sandeep Bhandari Jul 04 '18 at 08:45
  • 1
    you can use less than or greater than constraint equalities – Devil Decoder Jul 04 '18 at 08:45
  • No run time constraints required, just set proper content hugging & compression resistence priority – SPatel Jul 04 '18 at 09:54

2 Answers2

3

If I am getting it right, your table view cell contains a label. You want the minimum height of the cell to be 106, and if the text content in your label increases such that the label doesn't fit anymore within the cell height, the height of the cell should increase. If this is the problem, then you need to use automatic dimension and for that you have to do two things

1) For automatic dimension to work you have to set layout constraints in such a way that there is at least one constraint connecting a cell subview and the top of contentView and at least one connecting a cell subview and the bottom of the contentView. Assuming there is just a label inside the cell, give leading, trailing, top and bottom constraints. You may give a 10px padding on all sides.

2) Set estimatedRowHeight for the table view as 106. 3) In heightForRowAtindexPath delegate method, return UITableViewAutomaticDimension

You are all set now.

angshuk nag
  • 451
  • 5
  • 11
1

Add one more constraint to label as: height: which is greater than or equal to your required height value.

Van
  • 1,225
  • 10
  • 18