Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Gonna answer my own question here:</p> <p>Originally I was pretty sure that the height calculations were tied to the <code>tableView:cellForRowAtIndexPath:</code> method, and couldn't be moved elsewhere. With a bunch of restructuring, though, I was able to get that stuff out of there and into <code>tableView:heightForRowAtIndexPath:</code>, which solves everything.</p> <p>For anyone else who's trying to get auto-height-adjusted cells to work, here's some code that might help:</p> <pre><code>// Inside tableView:cellForRowAtIndexPath: cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; cell.textLabel.numberOfLines = self.numberOfTextRows; // numberOfTextRows is an integer, declared in the class - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap]; // This gets the size of the rectangle needed to draw a multi-line string self.numberOfTextRows = round(theSize.height / 18); // 18 is the size of the font used in the text label // This will give us the number of lines in the multi-line string if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows &lt; 2)) { return 44; // 44 is the default row height; use it for empty or one-line cells (or in other table sections) } else { return theSize.height + 16; // 16 seems to provide a decent space above/below; tweak to taste } } </code></pre> <p>If you can think of a more accurate way to calculate the proper cell height, I'm all ears. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload