Hi there is plenty of question answering the dynamic height for UITableViewCell
of UITableView
. However I find it weird when I did it.
here's are some of the answer :
usually this would answer the dynamic height for cell
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
but in my case I wonder this line wont do anything.
my UITableView
is being viewed after clicking tabbar inside the splitview. Is this helpful?
Maybe I'm Missing something. Could anyone help me I spent 2 hours doing silly.
These are my constraint for title the title could be long but the label is not.
and this is my cell
For my setup, I first double checked the constraints in the Storyboard for the
UITableViewCell
were unambiguous top to bottom, and then had to modify code in 2 places.UITableViewDelegate
'stableView(_:heightForRowAt:)
UITableViewDelegate
'stableView(_:estimatedHeightForRowAt:)
Step 1 and 2, lets you to apply autosizing just for particular rows. For applying to the whole
UITableView
, use:To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout.
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeightheightForRowAt
and return a valueUITableViewAutomaticDimension
to it)-
Objective C:
Swift:
For label instance in UITableviewCell
Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.
In order to make UITableViewAutomaticDimension to work, you need to make sure all 4 corners' constraints is added to the superview. In most case, it works well with UILabel, but sometimes i find UITextView does the trick when UILabel not working well at the time. So try to switch to UITextView and make sure
Scrolling Enabled
is unchecked in storyboard, or you can also set it programmatically.Try this, Simple solution it's work for me,
In viewDidLoad write this code,
In cellForRowAtIndexPath write this code,
I had three horizontal items and the most left had a constraint to left, middle one had top, bottom, to the left item and to the right item. The right one had a right, and left to the middle item. The solution was to add additional left and right constraints to the middle item (my label) that were >= some number to the cell.
I have a specific case, and 99% of the solutions I read didn't work. I thought I'd share my experience. Hope it can helps, as I'd struggled for a couple of hours before fixing this issue.
My scenario:
What I needed to achieve:
What you'll need to check to make it work smoothly:
Like every tutorial and answer will tell you, set all the constraints for your label/content (top, bottom, leading & trailing), and set it to the ContentView (Superview). This video tutorial will be helpful.
In the
viewWillAppear
method of my ViewController, I am giving one of my tableViews (tableView2
) an estimated row height and then use theUITableViewAutomaticDimension
as such (and as seen in all tutorials/answers):For me, these previous steps were not enough. My TableView would simply take the estimatedRowHeight and multiply it by the number of cells. Meeeh.
As I am using two different tableViews, I created an outlet for each of them,
tableView1
andtableView2
. Allowing me to resize them in theviewDidLayoutSubviews
method. Feel free to ask in comment how I did this.One additional step fixed it for me, by adding this to the
viewDidAppear
method:Hope that helps someone!