Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UITableView's rowHeight to UITableViewAutomaticDimension in storyboard?

When creating an app for iOS 8 in Xcode 6, how do I set my UITableView's rowHeight to UITableViewAutomaticDimension?

In WWDC 2014 Session 226 "What’s New in Table and Collection Views", the speaker says that UITableViewAutomaticDimension is the new default value for programmatically created table views' rowHeight. However, he also mention that at the time of the session this is not the case for table views loaded from a xib/storyboard.

Now that Xcode 6 GM is out, how do I set this value in the storyboard, without having to add

self.tableView.rowHeight = UITableViewAutomaticDimension;

in my viewDidLoad?

like image 777
Guillaume Algis Avatar asked Sep 17 '14 10:09

Guillaume Algis


1 Answers

Xcode 11+

Starting with Xcode 11, Interface Builder now has an "automatic" checkbox near the "Row Height" input.

As stated in the release notes :

Cells in a UITableView can now self size with Auto Layout constrained views in the canvas. To opt into the behavior for existing table views, enable “Automatic” for the table view estimated item size, and “Automatic” for cell’s height in the Size inspector. (35735970)

Xcode 11 Interface Builder


Pre Xcode 11

After testing different values in the storyboard, the solution is simply to keep the default 44pt value in Row Height:

Xcode 6 Interface Builder

What this probably means is that you can't have fixed height cells of 44pt anymore set in the storyboard. You'll have to set it programmatically in viewDidLoad or elsewhere.

Finally, note that its currently not possible to set an estimatedRowHeight anywhere in storyboard, and that if you do not set it programmatically, it will default to 0. (edits welcome if you find out how to do this)


You can test the 44pt behavior yourself by logging the values in viewDidLoad:

NSLog(@"%f", UITableViewAutomaticDimension);
NSLog(@"%f", self.tableView.rowHeight);

With Row Height set to 44 this produces:

-1.000000

-1.000000

With Row Height set to another value (here 45pt):

-1.000000

45.00000

like image 131
Guillaume Algis Avatar answered Oct 17 '22 05:10

Guillaume Algis