0

I am on Xcode 8 with Swift 3 building a ViewController with a tableview and a custom cell. I have multiple labels within a custom cell and I have set auto layout in my initialization with following code:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 90.0

This is how it's set up on Xcode:

My view controller with tableview and custom cell

This is how it looks on simulator:

on simulator

I have tried setting constraints on the last label on the right with the same result at runtime. Do I need to put the views in a container view like CollectionView or something?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Katlock
  • 1,200
  • 1
  • 17
  • 41
  • 2
    You need a _complete_ set of constraints running from the top of the content view down through every label all the way to bottom of the content view. I see no evidence that you have done that. – matt Aug 15 '17 at 16:26
  • Duplicate of https://stackoverflow.com/questions/8615862/custom-cell-row-height-setting-in-storyboard-is-not-responding – JSBach Aug 15 '17 at 16:30
  • You need to make sure you have constraint on the bottom, like your bottom labels should have bottom constraint to the bottom of the cell otherwise the automatic calculation wont work – Emil Aug 15 '17 at 16:43
  • I am able to get cell row to show all the labels with setting up constraints and playing around with it like others have suggested. Thanks – Katlock Aug 15 '17 at 16:59
  • @Sonam Check 2 things for label . 1) is Lines set to 0 or not 2) have you given height constraint to labels then you need to make sure it has property >= set on it.link-: https://stackoverflow.com/questions/45680791/getting-the-height-of-a-label-in-custom-cell-to-use-for-heightforrowat-in-my-mai?noredirect=1#comment78319803_45680791 – Tushar Sharma Aug 15 '17 at 17:39

3 Answers3

1

It is not clear what constraint you have imposed. However, You must need to pin the first label to the top of the superview and last label to the bottom of the superview, to make the automatic dimension work. Also, make sure all the label have set their top and bottom to each other.

Shubham
  • 763
  • 5
  • 20
1

Set the auto-constraint for the top, bottom, leading and trailing to whatever value you'd like. This value is however many pixels you would like to be off of the different margins. After that, add the following code to viewDidLoad():

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 100

By using UITableViewAutomaticDimension you are telling the table view to use the Auto Layout constraints which will use the contents to determine the height.

To use UITableViewAutomaticDimension you need to specify a estimatedRowHeight. This value is an arbitrary value that will be the fallback, and you can use whatever value you think would be good for your project. Let me know if this works for you.

Grady
  • 174
  • 6
  • 21
1

Set your controller to be the tableview delegate.

Implement tableView(_:heightForRowAt:) method from the UITableView protocol and

return 90.0