title: How to know which cell was tapped in tableView using Swift
body: <p>I am using a tableView to display a list of names. When the user taps on one of the cells, a popup should come up showing more detail. I am using this function to check what cell the user has tapped.</p>
  
  <pre><code>var selectedCell: Int?
  
  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("This cell from the chat list was selected: \(indexPath.row)")
      selectedCell = indexPath.row
  }
  </code></pre>
  
  <p>Unfortunately, it seems to give give back random numbers between 0 and 2 (I currently have three cells that are displayed).</p>
  
  <p>Do you know how I could know which cell was tapped?</p>
  
  <p>Thank you in advance.</p>
  
  <p>EDIT:</p>
  
  <p>I understood what is falsing my results.</p>
  
  <p>I am using this function to push which cell is selected to another view controller (named ConversationViewcontroller). But, the segue fonction is called before the tableView function, and so the segue pushes the previously selected cell. How could I correct this?</p>
  
  <p>Here is the code for my segue:</p>
  
  <pre><code>//SEGUE: Cell was selected: pass chatID
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "OpenChat" {
              if let toViewController = segue.destination as? ConversationViewController {
                  toViewController.chatIdentifier = selectedCell
              }
          }
      }
  </code></pre>
  
  <p>Thanks again.</p>