title: Animate adding a subview in a cell with dynamic cell height
body: <p>I would like to animate the <code>addSubview</code> of a subview in a cell. The tableview cells have dynamic height, so the animation for collapse and expand is done natively.</p>
  
  <p>This is the code:</p>
  
  <pre><code>// MARK: UITableViewDelegate
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let selectedCell = tableView.cellForRow(at: indexPath) as? MyCell {
      selectedCell.showExtraView()
      expandedCells.insert(indexPath)
    }
    if let prevSelectedCell = tableView.cellForRow(at: prevIndexPath) as? MyCell {
      prevSelectedCell.hideExtraView()
      expandedCells.remove(prevIndexPath)
    }
  }
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
    if expandedCells.contains(indexPath) {
      return CGFloat(100.0)
    } else {
      return CGFloat(80.0)
    }
  }
  </code></pre>
  
  <p>The cell code:</p>
  
  <pre><code>// MyCell
  
  func showExtraView() {
    contentView.addSubview(extraView)
    contentView.setNeedsLayout()
  }
  
  func hideExtraView() {
    extraView.removeFromSuperview()
    contentView.setNeedsLayout()
  }
  </code></pre>
  
  <p>This is what it looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/8MPQy.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8MPQy.gif" alt="enter image description here"></a></p>
  
  <p>The subview (<code>extraView</code>) includes the image and the label. The label animates correctly, but the image just appears there as if it's not part of the animation. </p>