Dhyani Shah Dhyani Shah - 1 year ago 151
iOS Question

How to set height of UITableView cell dynamically using Autolayout?

I had taken two view inside content view.
Height of "Post Container View" (Red background) is calculated dynamically as per height of label (All done using Autolayout).

Now I want that if height of "Post Container View" (Red background) will increase then height of cell view auto increase. I want to do this using autolayout.

I want to calculate height of UITableview cell using Autolayout. How to do it ?

Cell Height = Post Container View (Flexible as per label height)+ Image Container View Height (300 Fix)

I had seen this type of method, but dont know how to implement in my code ?

- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell

sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.MyTableView.frame), CGRectGetHeight(sizingCell.bounds));

[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];

CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1.0f; // Add 1.0f for the cell separator height

Answer Source

To calculate height of cell :

CGFloat height ; // take global variable

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      height = cell.PostContainerView.frame.size.height ;

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
    height = height + 300 //(300 is Fix height of Image Container View ) ;
    return height ;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download