Search code examples
dynamicautolayoutheightconstraintscell

How do I make constraints to UITableView with multiple cells with static and dynamic height?


I searched a lot about this, couldn't find an answer and I am stuck with this problem for some days already.

I have a UITableView with 3 sections, each one with custom cells (xib). The first section must grow accordingly with one of its labels. The second one is static, it shows up just an static image and basic height. The third one is like a comment cell, with UIImageView, labels etc. One of this labels must grow till a limit height.

I managed to show the 3 custom cells, but the first and the second one (which are not static height), show only partially. If I set manually the Height of the row I can see it entirely, but all section cells grow and it is not what I need.

The constraints to auto layout, I keep getting some conflicts but could not find any solution, tried to debug the view stack, no success.

Custom Cell

Constraints of custom cell

Can anyone help me or point me to a direction?

Thanks in advance!


Solution

  • I had the same problem. I solved it like this. Set tableView estimatedRowHeight to minimum row height. Set tableView rowHeight to UITableViewAutomaticDimension.

    1) For dynamic cell height - Give the label leading, trailing, top, bottom and height greater than equals to minimum height.

    2) For fixed cell height - Give the label leading, trailing, top, bottom and height equals to fixed height whatever you wanted.

    By this your dynamic height cells will grow and fixed ones will be fixed. If you did not get what i said then post a sample project with just this custom implementation on git or somewhere else. I will correct it.

    Thank you.