Note that there are some explanatory texts on larger screens.

plurals
  1. POheightForRowAtIndexPath for grouped UITableViewCell
    text
    copied!<p>I'm using the following method implementation to calculate the height of a <code>UITableViewCell</code> which is containing multiline text: </p> <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1 &amp;&amp; indexPath.row == 1) { NSDictionary *fields = self.messageDetailsDictionary[@"fields"]; NSString *cellText = fields[@"message_detail"]; UIFont *cellFont = [UIFont systemFontOfSize:14.0]; CGSize constraintSize = CGSizeMake(250.0f, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; return labelSize.height + 20; } else { return tableView.rowHeight; } } </code></pre> <p>For completeness, here's the <code>cellForRowAtIndexPath</code> entry for this cell:</p> <pre><code> UITableViewCell *cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"]; if (cell == nil) { cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"]; } cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; cell.textLabel.font = [UIFont systemFontOfSize:14.0]; NSDictionary *fields = self.messageDetailsDictionary[@"fields"]; cell.textLabel.numberOfLines = 0; // This means multiline cell.textLabel.text = fields[@"message_detail"]; return cell; </code></pre> <p>The <code>UITableViewCell</code> is in a Grouped <code>UITableView</code>, that's important because it affects the width of the cell.</p> <p>This is working to the extent that it does calculate a cell height that is large enough to accomodate the text being input, but it seems to be a little too large as there is a little too much space at the top and bottom of the cell. This is dependent on the quantity of text so I don't think it's related to the <code>return labelSize.height + 20;</code> statement. I suspect this is down to the '250.0f' value I'm using in <code>CGSizeMake</code> but I don't know what the correct value should be here.</p> <p>Ultimately what I want is to have a cell that has consistent padding above and below the text for any content size.</p> <p>Can anyone help?</p>
 

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