iOS8: Possible to use "tableview.rowHeight = UITableViewAutomaticDimension" for static cells? iOS8: Possible to use "tableview.rowHeight = UITableViewAutomaticDimension" for static cells? ios ios

iOS8: Possible to use "tableview.rowHeight = UITableViewAutomaticDimension" for static cells?


For iOS 8, I found out that you cannot do the same strategy as for Dynamic Table View Cell.

To automatically adjust the height of static cells, I implement these two UITableViewDelegate methods:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewAutomaticDimension;}-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 44;}

Hope it helps.


Expanding over Victors' answer, Static Cells based table views seem to autosize cells based on content and constraints once the following delegates are implemented:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewAutomaticDimension;}-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 44;}

EXCEPT for when there are labels. For some reason the label intrinsic content heights do not seem to contribute towards the height calculation of the cell when they have . My current work around for this is to nest the labels in an UIView.

Do the following:

  1. Embed the label (or labels) in a view. (Select label(s) on IB and hit Menu > Editor > Embed In > View)
  2. Establish horizontal and vertical margin constraints between thisview and the cell
  3. Establish horizontal and vertical margin constrains between yourlabel(s) and this view.

Certainly feels like a hack but works for me in all the cases I have tried.


Ended up doing this. Not what I wanted, but it kind of works for my simple requirements and text lengths.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.row == 2)    {        CGFloat padding = 30.0f;        CGFloat labelWidth = self.tableView.bounds.size.width - padding*2;        NSAttributedString *text = [[NSAttributedString alloc] initWithString:_freeTextLabel.text];        NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;        CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)                                             options:options                                             context:nil];        return (CGFloat) (ceil(boundingRect.size.height) + padding*2);    }    return [super tableView:tableView heightForRowAtIndexPath:indexPath];}