title: Expand and contract tableview cell when tapped, in swift
body: <p>I'm trying to expand a tableview cell when tapped. Then, when it is tapped again, I want it to go back to its original state.</p>
  
  <p>If cellA is expanded and cellB is tapped, I want cellA to contract and cellB to expand at the same time. So that only one cell can be in its expanded state in any given moment.</p>
  
  <p>My current code:</p>
  
  <pre><code>class MasterViewController: UITableViewController {
     var isCellTapped = false
     var currentRow = -1
  
  
     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          selectedRowIndex = indexPath
          tableView.beginUpdates()
          tableView.endUpdates()
      }
  
  
     override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
  
      if indexPath.row == selectedRowIndex.row {
          if isCellTapped == false {
              isCellTapped = true
              return 140
          } else if isCellTapped == true {
              isCellTapped = false
              return 70
          }
  
      }
  
      return 70
  }
  </code></pre>
  
  <p>Current code works well when:</p>
  
  <ul>
  <li>You tap a row (it expands)</li>
  <li>You tap it again (it contracts)</li>
  </ul>
  
  <p>It fails when:</p>
  
  <ul>
  <li>You tap a row (it expands)</li>
  <li>You tap another row (it contracts, but the other row does not expand)</li>
  </ul>
  
  <p>How can I solve this?</p>