Try overriding the 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 the UITableView, known as cells for sizing. Prior to iOS 8, if you displayed dynamic content in the form of a table using a different row, you need to calculate the row height yourself.
In short, here are the steps you need to implement when using self-sized cells:
β’ Add auto-layout constraints in the prototype cell
β’ Indicate the approximate RowHeight view of your table view.
β’ Set rowHeight of your table view to UITableViewAutomaticDimension
Expressing the last two points in the code, it looks like this:
tableView.estimatedRowHeight = 43.0; tableView.rowHeight = UITableViewAutomaticDimension
You must add it to the viewDidLoad method.
With just two lines of code, you instruct the table view to calculate the size of the cells corresponding to its contents and make it dynamic. This cell calibration function should save a ton of code and time.
In the Attributes Inspector of your UILabel, change the Lines value to 0, so the label will automatically adjust the number of lines according to the content.
Note that the first point is required, and remember that you cannot use your own size cell without using an automatic layout.
If you notice a familiarity with automatic layout, read this, this will be enough:
https://developer.apple.com/library/mac/recipes/xcode_help-IB_auto_layout/chapters/pin-constraints.html
Or, an easier way to set the automatic layout, but maybe not as you expected, is to clear all your restrictions, go to the "Allow automatic layouts" section, and for all views, click "Reset" on the "Suggested restrictions".