Jace Jace - 1 year ago 196
iOS Question

How to create static cells with dynamic cell heights with Swift

I have looked at several tutorials that show how to set dynamic cell heights, but all of them only show it if you are using dynamic cells by setting the appropriate constraints and using

. However, I would like to do this for static cells.

I have a table view controller in my app that contains several cells that display a category with a disclosure indicator in which there is a title and then a description and the size of the description text varies. Thus, I would like the height of the cell to be dynamic per the size of the description text.

The reason why I am using static cells and not dynamic cells is because above the category cells I have a few cells with some designs that I could only get by using static cells and storyboarding.

I am using
Xcode 7.3
Swift 2.2


It was suggested that I setup the constraints and use
basically the exact same way it is normally done for dynamic cells. However, I did try this before posting my question and it is not working for me. Below are the details of what I have done.

enter image description here

Note: I noticed that when I select the cell, the size inspector by default had the Custom height checkbox checked and the height of the Spain category cell was set to 104.
enter image description here
If I run the app with the custom height set to 104 then the size is 104 no matter what.
So I unchecked the custom height checkbox which then shrunk the cell in the storyboard to the default size of a cell when you first add it from the object library and the results when I run it are shown at the end of this update.


Note that I have used a top, bottom, leading and trailing constraint for all my labels and I also doubled checked that the number of lines for each label is set to 0.

Spain title label:

enter image description here

Spain description label:

enter image description here

Arrow Image (simulating disclosure indicator, fixed height and width):

enter image description here

Container View: (I have a container view stretching the full size of the cell, could this be the problem?)

enter image description here

Table View Controller Code

import UIKit

class CategoriesTableViewController: UITableViewController {

override func viewDidLoad() {

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 2


Build and Run
enter image description here

Answer Source

You are using text view of each cells. Okay , Let select your text view and uncheck the scrolling enabled like as below image.

enter image description here

add below code,to your view controller

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

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download