title: Segue not getting selected row number
body: <p>I am passing data from a table view controller to a detail view. I tried using <code>indexPath.row</code> directly in my <code>prepareForSegue</code> method, however it displays an error of </p>
  
  <blockquote>
    <p>use of unresolved identifier 'indexPath'</p>
  </blockquote>
  
  <p>So, after searching the web, I set up the variable <code>indexOfSelectedPerson</code> which is assigned the value of <code>indexPath.row</code>. The problem when I run the app in the simulator is that prepareForSegue is getting the initial value of <code>indexOfSelectedPerson</code> (0), then getting the value of the selected row only after I click it. So, when I hit the back button in the Simulator and select a different row, the detail view shows the info of the row I selected the previous time.</p>
  
  <pre><code>import UIKit
  
  class MasterTableViewController: UITableViewController {
  
      var people = []
      var indexOfSelectedPerson = 0
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          people = ["Bob", "Doug", "Jill"]
      }
  
      override func numberOfSectionsInTableView(tableView: UITableView?) -&gt; Int {
         return 1
      }
  
      override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -&gt; Int {
          return people.count
      }
  
      override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -&gt; UITableViewCell! {
  
          let cell = tableView!.dequeueReusableCellWithIdentifier("personCell", forIndexPath: indexPath) as UITableViewCell
  
          cell.text = "\(people[indexPath.row])"
  
          return cell
      }
  
      override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
      {
          indexOfSelectedPerson = indexPath.row
      }
  
  
      override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
  
          if let mySegue = segue.identifier {
  
              if mySegue == "personDetails" {
  
                  let detailsVC: DetailTableViewController = segue.destinationViewController as DetailTableViewController
  
                  detailsVC.selectedPersonName = "\(people[indexOfSelectedPerson])"
              }
          }
      }
  }
  </code></pre>
  
  <p>So, selecting Doug when the app first starts in the simulator displays the details for Bob because <code>indexPathOfSelectedPerson</code> is 0. Hitting the back button and then selecting Jill displays the details for Doug because <code>indexPathOfSelectedPerson</code> became 1 when I clicked on Doug the previous time. I'm guessing the problem stems from the order in which the methods are called.</p>