Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS dynamical height of UITableViewCell and heightForRowAtIndexPath

I'm using Autolayout for my new UITableViewCells in a large project.

I've one TableView where the height of each row is calculated automatically, there I don't use the delegate function heightForRowAtIndexPath.

I've declared a estimated row height:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

My question is: on another TableViewController there are a lot of UITableViewCells, where I programmatically need to declare the height of the cell in heightForRowAtIndexPath. I know that It would be better to convert all cell's to use a unique solution, but in this project are a lot of different cell's, so I'd like to use a workaround and combine the dynamically calculated height with autolayout and the programmatically calculated row height.

Is this possible?

like image 714
brokedid Avatar asked Jul 10 '15 08:07

brokedid


1 Answers

If you are using iOS 8 and above, you do not need to calculate height dynamically. Auto layout will do all for you. But if you are using lower than IOS 8, you need to calculate cell height.

For IOS 8:

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

And add below code in your controller:

tableView.estimatedRowHeight = 400.0
tableView.rowHeight = UITableViewAutomaticDimension

Where estimatedRowHeight should be max height which can be for your cell.

Thanks

like image 135
Hindu Avatar answered Nov 03 '22 02:11

Hindu