Search code examples
objective-cuitableviewios9uitableviewautomaticdimension

iOS: Dynamic Height For UITableViewCell


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


Solution

  • 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;
    }