iOS: Dynamic Height For UITableViewCell

Ankit picture Ankit · May 4, 2016 · Viewed 9.2k times · Source

I'm using iOS9 XCode7 I need to change the height of cell Dynamically according to labelText Height

I have used:

self.tableView.rowHeight=UITableViewAutomaticDimension;

But it is not working for custom made cell. sizetoFit is removed in iOS9.

Please suggest something. Thanks

Answer

Harish Pathak picture Harish Pathak · May 4, 2016

Give your label constrains relative to the cell, top, bottom, left and right.

Than your cell size will grow with the content height.

also make your label multiline.(by setting Lines property to 0 in attribute inspector)

#pragma mark- Menu table Delegates 

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewAutomaticDimension;
}