Try to override methods:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Complete solution:
In iOS 8, Apple introduces a new feature for UITableView known as Self Sizing Cells. Prior to iOS 8, if you displayed dynamic content in table view with varied row, you need to calculate the row height on your own.
In summary, here are the steps to implement when using self sizing cells:
• Add auto layout constraints in your prototype cell
• Specify the estimatedRowHeight of your table view
• Set the rowHeight of your table view to UITableViewAutomaticDimension
Expressing last two points in code, its look like:
tableView.estimatedRowHeight = 43.0;
tableView.rowHeight = UITableViewAutomaticDimension
You should add it in viewDidLoad method.
With just two lines of code, you instruct the table view to calculate the cell’s size matching its content and render it dynamically. This self sizing cell feature should save you tons of code and time.
In Attributes Inspector of your UILabel, change Lines value to 0, so label will automatically adjust the number of lines to fit the content.
Please note that first point is required and remember that you cannot use self sizing cell without applying auto layout.
If you are note familiar with auto layout, please read this, it will be enough:
https://developer.apple.com/library/mac/recipes/xcode_help-IB_auto_layout/chapters/pin-constraints.html
Or, easier way to set auto layout, but maybe not be what you exactly expected is to clear all of your constraints, go to Resolve Auto Layout Issues and for All Views click on Reset to Suggested Constraints.