Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a UITableView that has five static cells. I need the cell height of one cell to adjust automatically to its contents, which is one UILabel.

Is there any way I can use..

tableView.estimatedRowHeight = 42.0
tableView.rowHeight = UITableViewAutomaticDimension

..for a table view with static cells, or does this only work for table views with dynamic prototype cells?

(Or is there any other way of doing this that you would recommend?)

Additional info

The UITableView is in a TableViewController which is embedded into a Container View.

The five static cells are quite different from one another and are only used in one app scene, so I don't see much point in going for dynamic prototypes and custom cell subclasses.

I update the tableview like this

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_myTableView reloadData];
}

I add constraints to the UILabel as described here: http://www.appcoda.com/self-sizing-cells/

like image 531
camelBase Avatar asked Oct 01 '14 08:10

camelBase


3 Answers

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.

like image 162
Victor Chandra Avatar answered Nov 01 '22 17:11

Victor Chandra


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 this view and the cell
  3. Establish horizontal and vertical margin constrains between your label(s) and this view.

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

like image 23
Apurva Avatar answered Nov 01 '22 19:11

Apurva


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];
}
like image 23
camelBase Avatar answered Nov 01 '22 17:11

camelBase