title: Swift: Auto layout and table view cells with dynamic heights
body: <p>I've created a test project to understand table cells with variable heights. I've read a number of tutorials on StackOverflow and elsewhere but I'm still doing something wrong, and I'm not quite sure what.</p>
  
  <p><strong>How do I ensure that regardless of the string length, the table cell's height changes to accommodate all of the text?</strong></p>
  
  <p>Here I set up my table view with an array of strings of various lengths.</p>
  
  <p>Here's what my simulator shows, as well as my code:</p>
  
  <p><img src="https://i.stack.imgur.com/9cQ2r.png" alt="tableview"></p>
  
  <pre><code>class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      var items: [String] = ["We", "Aenean lacinia bibendum nulla sed consectetur.", "Vestibulum id ligula porta felis euismod semper. Maecenas sed diam eget risus varius blandit sit amet non magna. Sed posuere consectetur est at lobortis. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cras mattis consectetur purus sit amet fermentum. Etiam porta sem malesuada magna mollis euismod. Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum"]
  
      @IBOutlet var tableView: UITableView!
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return self.items.count
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
  
          cell.textLabel?.text = self.items[indexPath.row]
  
          return cell
      }
  
      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          println("You selected cell #\(indexPath.row)!")
      }
  
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
  
          self.tableView.rowHeight = UITableViewAutomaticDimension
          self.tableView.estimatedRowHeight = 130.0
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func prefersStatusBarHidden() -&gt; Bool { return true }
  }
  </code></pre>
  
  <p>In my storyboard, I have a single test cell set up with some constraints (which may be done incorrectly, not sure). Constraints seem to be the crux of nailing this implementation or totally screwing it up</p>
  
  <p><img src="https://i.stack.imgur.com/dDXfC.png" alt="contraints"></p>
  
  <p>Any help would be hugely appreciated. Thanks!</p>