title: Swift 3 - Expandable Table View Cells with first cell already expanded
body: <p>I am using Swift 3.</p>
  
  <p>I've followed this tutorial to get it so that I can tap on a table view cell which will expand revealing more information.  </p>
  
  <p><a href="https://www.youtube.com/watch?v=VWgr_wNtGPM&amp;t=294s" rel="nofollow noreferrer">https://www.youtube.com/watch?v=VWgr_wNtGPM&amp;t=294s</a></p>
  
  <p>My question is: how do I do it so that the first cell is expanded when the view loads already (i.e. the user doesn't have to click to see that cell expand) but all other behavior remains the same (e.g. if it's clicked again, it de-collapses)?</p>
  
  <p><code>UITableViewCell</code>: </p>
  
  <pre><code>import UIKit
  
  class ResultsCell: UITableViewCell {
  
      @IBOutlet weak var introPara : UITextView!
      @IBOutlet weak var section_heading : UILabel!
  
      class var expandedHeight : CGFloat { get { return 200.0 } }
      class var defaultHeight : CGFloat { get { return 44.0 } }
      var frameAdded = false
  
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
          section_heading.translatesAutoresizingMaskIntoConstraints = false
      }
  
      func checkHeight() {
          introPara.isHidden = (frame.size.height &lt; ResultsCell.expandedHeight)
      }
  
      func watchFrameChanges() {
          if(!frameAdded) {
              addObserver(self, forKeyPath: "frame", options: .new, context: nil)
              checkHeight()
          }
      }
  
      func ignoreFrameChanges() {
          if(frameAdded){
              removeObserver(self, forKeyPath: "frame")
          }
      }
  
      deinit {
          print("deinit called");
          ignoreFrameChanges()
      }
  
      // when our frame changes, check if the frame height is appropriate and make it smaller or bigger depending
      override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
          if keyPath == "frame" {
              checkHeight()
          }
      }
  
  }
  </code></pre>
  
  <p><code>UITableViewController</code></p>
  
  <pre><code>// class declaration and other methods above here...
  
  override func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  
  // number of rows in the table view
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return section_heading.count
  }
  
  // return the actual view for the cell
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let resultcell = tableView.dequeueReusableCell(withIdentifier: "resultCellTemplate", for: indexPath) as! ResultsCell
      resultcell.section_heading.text = section_heading[indexPath.row]
      resultcell.introPara.attributedText = contentParagraphs[indexPath.row]
  
      return resultcell
  }
  
  // when a cell is clicked
  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      let previousIndexPath = selectedIndexPath
  
      // the row is already selected, then we want to collapse the cell
      if indexPath == selectedIndexPath {
          selectedIndexPath = nil
      } else { // otherwise, we expand that cell
          selectedIndexPath = indexPath
      }
  
      var indexPaths : Array&lt;IndexPath&gt; = []
  
      // only add a previous one if it exists
      if let previous = previousIndexPath {
          indexPaths.append(previous)
      }
      if let current = selectedIndexPath {
          indexPaths.append(current)
      }
  
      // reload the specific rows
      if indexPaths.count &gt; 0 {
          tableView.reloadRows(at: indexPaths, with: .automatic)
      }
  }
  
  override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
      (cell as! ResultsCell).watchFrameChanges()
  }
  
  override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
      (cell as! ResultsCell).ignoreFrameChanges()
  
  }
  
  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      if indexPath == selectedIndexPath {
          return ResultsCell.expandedHeight
      } else {
          return ResultsCell.defaultHeight
      }
  }
  </code></pre>
  
  <p>So this works as intended.</p>
  
  <p><a href="https://i.stack.imgur.com/AnTOp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AnTOp.png" alt="enter image description here"></a></p>
  
  <p>But how do I make it so that the first cell is already expanded?</p>
  
  <p>Thanks for your help.</p>