Syed e Hussaini Syed e Hussaini - 5 months ago 14
iOS Question

TableView cell size to with respect to label size

I have a

UITableView
and have my own custom cell containing a
UILabel
inside the cell.

I want to increase the tableView row size according to the text in the label.
I am using AutoLayout and my label have 20 padding from left and 8 padding from right,top and bottom.

I am stuck here because the label context is some time small and sometimes very large and i want to increase the size of that row when the text is increased.

Answer

Set the UILabel.numberOfLines to 0 and add this to your UITableViewDelegate

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
Comments