Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Custom UITableViewCell Custom UITableViewCell with Dynamic Height

Thomas Euget
Thomas Euget
10,615 Points

Is it possible to update this lesson to the latest version of Xcode / iOS, it doesn't work.

I am using latest version of Xcode/ios. Despite following instructions the cell won't adapt its height to the content of the cell. Would it be possible to help me on this one since I have no idea how to solve it. Thanks vm.

6 Answers

I haven't read it through, but this link will probably help you understand why it doesn't function and how to solve it.

Robbert Brouwers
Robbert Brouwers
10,035 Points

The answer is indeed in there Eirik, however implementing is unfortunately not a matter of copy paste; don't know where and how to implement it.

According the appcoda tut it comes down to replacing the code from the video with these two lines:

tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension

But how are these correctly implemented?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Putting those in viewDidLoad should be sufficient – let me know if it works.

Robbert Brouwers
Robbert Brouwers
10,035 Points

I've tried to add it to the viewDidLoad in THEntryListViewController.m but get an error: "Unknown type name 'tableView': did you mean UITableView'?"

Do I have to declare/import anything in order for the tableView to be used properly?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Ah, gotcha. The above code is in Swift, not Objective-C. For Objective-C, you'll need the use the following:

self.tableView.estimatedRowHeight = 68.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

Let me know if that works.

Robbert Brouwers
Robbert Brouwers
10,035 Points

Well, the code is working, however it doesn't change the format of the table in simulator, which messes things up:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.tableView.estimatedRowHeight = 68.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    [self.fetchedResultsController performFetch:nil];
}

picture of simulator here: http://nl.tinypic.com/r/2gws75g/8

ps. I appreciate you're taking the time to look into this!

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Hmm, very curious. Could I ask you to upload a zipped copy of the Xcode project somewhere, or email it to me at ash@ashfurrow.com so I can take a look?

Stephen Peltier
Stephen Peltier
9,681 Points

I was having the same issue and think I figured it out. Did you set UI constraints to the items in your cell? The link above says to add the two lines of code, and also to set the auto layout for the cell

After re-enabling auto layout and putting in some constraints the results I get look like they should.

Ash Furrow
STAFF
Ash Furrow
Treehouse Guest Teacher

OK I've taken a look (apologies for the delay – moving to NYC from Europe has been quite time-consuming). Yes, just setting the rowHeight won't work unless you use auto layout constraints, which Stephen Peltier correctly points out that we're not using.

However, continuing to use the non-auto layout method should work. You need to create the heightForRowAtIndexPath method of UITableViewDelegate's, then call the heightForEntry: method on the cell class to calculate its height. Is that method not working?

can i get a look , like what is like ? i mean the code for height problem for non-auto layout