2

What I would like to do:

In the UITableViewController method heightForRowAtIndexPath I would like to infer the value of the row height rather than hardcoding it.

I set the value previously in the storyboard in Interface builder.

Wrong approach:

The following method is the wrong approach as it is called before the actual cell is created and hence it loops:

// wrong approach
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //AppTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppCel" forIndexPath:indexPath];
    //int height = cell.frame.size.height;
    // TODO: Make it more dynamic
    return 112;
}

Approach I am thinking of:

Is there a way to load the cell main.storyboard document and infer the height value? Or is there some other approach?

Related questions search:

I found the following unanswered related question dating back to 2010. This other question is not of much use as it derives the height from the cell content rather than from the story board document.

Community
  • 1
  • 1
mm24
  • 8,150
  • 11
  • 65
  • 159

3 Answers3

3

The approach you had coded is actually not totally wrong. What is wrong is using dequeueReusableCellWithIdentifier:forIndexPath. Instead you want to use the simpler version dequeueReusableCellWithIdentifier which you can use to provide a prototype cell which you can configure, layout and then calculate the height.

If using purely iOS8, all this is done for you with the new auto layout. If you want to support iOS7 as well, then you need to do this manually.

The following link explains the process you need to use. I use this and it works great when you want to support both iOS7 and iOS8.

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

You also have to watch if you use size class based constraints in your cells as there is a less than obvious issue you have to work around to get the height correct. See Offscreen UITableViewCells (for size calculations) not respecting size class?

Community
  • 1
  • 1
Rory McKinnel
  • 7,730
  • 2
  • 15
  • 28
0

Yes you can.

You can use autolayout constraints and let the autolayout do the calculations for you.

check out AppCoda tutorial for this Link

YYfim
  • 1,347
  • 1
  • 8
  • 21
  • this approach does not work properly as I can't set the row height in autolayout – mm24 Feb 16 '15 at 11:31
  • you don't have to set the row height, just the estimatedRowHeight – YYfim Feb 16 '15 at 11:32
  • but in this way is the same as hardcoding it.. I wanted to skip writing the height – mm24 Feb 16 '15 at 11:35
  • it's not like hardcoding it. when you set the estimated valuem the true height can be calculated by the autolayout costraint. any way i use it in my apps and it works. – YYfim Feb 16 '15 at 11:41
  • estimatedRowHeight is only used by the table to work out the scroll bar sizing. Auto layout only works in iOS8. For iOS7 you have to do the work yourself. However all methods rely on you having proper constraints setup for height or nothing works. – Rory McKinnel Feb 16 '15 at 16:52
0

If you have the cell in a xib (CP_Cell.xib) you can do that:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return ((CP_Cell *) [[NSBundle mainBundle] loadNibNamed:@"CP_Cell" owner:self options:nil].lastObject).frame.size.height;
 }
peig
  • 418
  • 3
  • 10
  • The cell is defined in the main.storyboard document.. is it possible to use that? – mm24 Feb 16 '15 at 11:30
  • 1
    Just use `dequeueCellWithReusableIdentifier`. See my answer. This will just work if the cell is in your storyboard. The version you used `withIndexPath` is tied to indexPath which makes the tableView try and create the cell which is what causes your loop. – Rory McKinnel Feb 16 '15 at 16:49