title: UITableViewCell height with dynamic subview swift
body: <p>I have a <code>UITableView</code> that <code>UITableViewCell</code> has a  dynamic <code>UILabel</code> which will be added based on data i get from the database.</p>
  
  <p>I'm using something like</p>
  
  <pre><code>let testing = ["A", "B", "C", "D"] // This array is dynamic data
  self.dictionaryTableView.estimatedRowHeight = 44
  self.dictionaryTableView.rowHeight = UITableViewAutomaticDimension
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "vocabCell", for: indexPath)
      var y = 0
      for var i in 0..&lt;self.testing.count {
          let lbl = UILabel(frame: CGRect(x: 0, y: y, width: 50, height: 25))
          lbl.text = self.testing[i]
          lbl.numberOfLines = 0
          cell.addSubview(lbl)
          y += 20       
      }
      return cell
  }
  </code></pre>
  
  <p>But <code>UITableViewCell</code> height does not stretch automatically to display all content cell. Please help
  Here is the result
  <a href="https://i.stack.imgur.com/ayiMc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ayiMc.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT</strong> I added constraint for the uilabel in UITableView cellForRowAtIndexPath, the constraints are working (I knew it when I expend the cell height), but cell height not automaticly strech out</p>
  
  <pre><code>var bottomAnchor: NSLayoutYAxisAnchor = NSLayoutYAxisAnchor()
  for var i in 0..&lt;self.testing.count {
      let lbl = UILabel()
      lbl.text = self.testing[i]
      lbl.numberOfLines = 0
      lbl.translatesAutoresizingMaskIntoConstraints = false
  
      cell.addSubview(lbl)
      lbl.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 16).isActive = true
      lbl.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
      lbl.heightAnchor.constraint(equalToConstant: 25).isActive = true
  
      if i == 0 {
          lbl.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
      } else {
          lbl.topAnchor.constraint(equalTo: bottomAnchor).isActive = true
      }
      bottomAnchor = lbl.bottomAnchor
  }
  return cell
  </code></pre>
  
  <p>Many thanks</p>