title: How to expand and collapse cell correctly like accordion in Swift?
body: <p>In my app I am trying to play audio files in <code>UITableView</code>. When I tap the cell, it has to expand and play audio. Then if I tap another cell, it should act like accordion, the previous cell must collapse and audio to be stopped. But my implementation doesn't do this:</p>
  
  <pre><code>import UIKit
  import AVFoundation
  
  class AudioTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
  @IBOutlet weak var tableView: UITableView!
  var audioPlayer: AVAudioPlayer?
  let mp3sPaths = NSBundle.mainBundle().pathsForResourcesOfType("mp3", inDirectory: nil)
  var filteredAudios = [AnyObject]()
  var mp3names:NSMutableArray = []
  var cellTapped:Bool = true
  var currentRow = 0;
  
  override func viewDidLoad() {
      super.viewDidLoad()
      for mp3 in mp3sPaths as Array&lt;String&gt; {
          var mp3name = mp3.lastPathComponent
          mp3names.addObject(mp3name.stringByDeletingPathExtension)
      }
      println(mp3names)
      println(mp3sPaths)
      self.tableView.reloadData()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -&gt; Int {
      if tableView == self.searchDisplayController!.searchResultsTableView {
          return self.filteredAudios.count
      } else {
          return self.mp3names.count
      }
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("AudioCell") as AudioCell
  
      if tableView == self.searchDisplayController!.searchResultsTableView {
          cell.titleLabel.text = filteredAudios[indexPath.row] as? String
  
      } else {
          cell.titleLabel.text = mp3names[indexPath.row] as? String
      }
  
      cell.playIcon.text = "▶️"
      return cell
  }
  
  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      var mp3file = mp3sPaths[indexPath.row] as NSString
      var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
      var error: NSError?
      audioPlayer?.stop()
      audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &amp;error)
      audioPlayer?.play()
      if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
          cell.playIcon.text = "◾️"
      }
  
      var selectedRowIndex = indexPath
      currentRow = selectedRowIndex.row
      tableView.beginUpdates()
      tableView.endUpdates()
  }
  
  func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
      if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
          cell.playIcon.text = "▶️"
      }
      audioPlayer?.stop()
  
      var selectedRowIndex = indexPath
      currentRow = selectedRowIndex.row
      tableView.beginUpdates()
      tableView.endUpdates()
  }
  
  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
      if indexPath.row == currentRow {
          if cellTapped == false {
              cellTapped = true
              return 70
          } else {
              cellTapped = false
              return 40
          }
      }
      return 40
  }
  
  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
      cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
      UIView.animateWithDuration(0.25, animations: {
          cell.layer.transform = CATransform3DMakeScale(1,1,1)
      })
  }
  
  func filterContentForSearchText(searchText: String) {
      filteredAudios = mp3names as Array
      filteredAudios.filter{($0 as NSString).localizedCaseInsensitiveContainsString("\(searchText)")}
  }
  
  func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -&gt; Bool {
      self.filterContentForSearchText(searchString)
      return true
  }
  
  func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -&gt; [AnyObject]! {
      var shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action, indexPath) -&gt; Void in
          tableView.editing = false
          println("shareAction")
      }
      shareAction.backgroundColor = UIColor.grayColor()
  
      var doneAction = UITableViewRowAction(style: .Default, title: "Done") { (action, indexPath) -&gt; Void in
          tableView.editing = false
          println("readAction")
      }
      doneAction.backgroundColor = UIColor.greenColor()
  
      var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -&gt; Void in
          tableView.editing = false
          println("deleteAction")
      }
  
      return [deleteAction, doneAction, shareAction]
  }
  
  func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
  }
  }
  </code></pre>
  
  <p>The screenshots are following:
  <img src="https://i.stack.imgur.com/qmFwT.jpg" alt="My Swift app"></p>
  
  <p>Can anybody help, please?</p>