title: Passing Index of a selected row to another ViewController
body: <p>I have a question concerning the passing of an index (myIndex) from a TableView to another ViewController.
  I have posted the code below. The good news: When I run the code, an Index is passed to the other Viewcontroller. The bad news: it is not the index I expected. (When I print out myindex in the Viewcontroller with TableView, everything works fine. If I pass myIndex to another ViewController, the index is mixed up...</p>
  <p>Target: My TableView consists of 18 rows (each hole a row). When I click on a row, another Viewcontroller will open up. In this VieController, the name of the selected row should be displayed on top.
  My idea was to pass the index of each row to the other Viewcontroller but that is not working correctly.</p>
  <p>Can anybody help me out?</p>
  <pre><code>import UIKit
  var holes = [&quot;Hole 1&quot;,&quot;Hole 2&quot;,&quot;Hole 3&quot;,&quot;Hole 4&quot;,&quot;Hole 5&quot;,&quot;Hole 6&quot;,&quot;Hole 7&quot;,&quot;Hole 8&quot;, &quot;Hole 9&quot;,&quot;Hole 10&quot;,&quot;Hole 11&quot;,&quot;Hole 12&quot;,&quot;Hole 13&quot;,&quot;Hole 14&quot;,&quot;Hole 15&quot;,&quot;Hole 16&quot;,&quot;Hole 17&quot;,&quot;Hole 18&quot;]
  var myIndex = 0
  
  class OverViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
      
      // Label
      @IBOutlet weak var BackButton: UIButton!
      @IBOutlet weak var ScoreButton: UIButton!
      @IBOutlet weak var OverviewLabel: UILabel!
      
  
      @IBOutlet weak var tableView: UITableView!
      
  
      override func viewDidLoad() {
             super.viewDidLoad()
      }
      
      
      @IBAction func BackToStartScreen(_ sender: UIButton) {
          performSegue(withIdentifier: &quot;BackToStart&quot;, sender: self)
      }
      
      @IBAction func GoToResultScreen(_ sender: UIButton) {
          performSegue(withIdentifier: &quot;GoToResult&quot;, sender: self)
      }
      
          
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
              return holes.count
      }
          
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: &quot;cell&quot;, for: indexPath)
              cell.textLabel?.text = holes[indexPath.row]
              return cell
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          myIndex = indexPath.row
          performSegue(withIdentifier: &quot;GoToScore&quot;, sender: self)
          
      }
    
  }
  </code></pre>
  <p>And the ScoreViewController:</p>
  <pre><code>import UIKit
  
  
  
  class ScoreViewController: UIViewController {
      var score = 0
      var fairwayhits = 0
      var greeninregulation = 0
      var putts = 0
      var text = &quot;&quot;
          
      // Variable Deklaration
      var CurrentScore = 0
      var CurrentFairwayHits = 0
      var CurrentGreenInRegulations = 0
      var CurrentPutts = 0
  
      
      
      // Bar Label
      @IBOutlet weak var BackLabel: UIButton!
      @IBOutlet weak var TitleLabel: UILabel!
      
      // Score Elements
      @IBOutlet var ScoreLabel: UILabel!
      @IBOutlet var ScoreStepper: UIStepper!
      @IBOutlet var ScoreResultLabel: UILabel!
      @IBOutlet var ScoreFrameLabel: UILabel!
      
      // Fairway Elements
      @IBOutlet var FairwayLabel: UILabel!
      @IBOutlet var FairwaySwitch: UISwitch!
      @IBOutlet var FairwayResultLabel: UILabel!
      @IBOutlet var FairwayFrameLabel: UILabel!
      
      // Green Elements
      @IBOutlet var GreenLabel: UILabel!
      @IBOutlet var GreenSwitch: UISwitch!
      @IBOutlet var GreenResultLabel: UILabel!
      @IBOutlet var GreenFrameLabel: UILabel!
      
      // Putt Elements
      @IBOutlet var PuttLabel: UILabel!
      @IBOutlet var PuttStepper: UIStepper!
      @IBOutlet var PuttResultLabel: UILabel!
      @IBOutlet var PuttFrameLabel: UILabel!
      
      // Save Button
      @IBOutlet var SaveButton: UIButton!
      
      // Back Button
      @IBAction func BackButtonPressed(_ sender: UIButton) {
          performSegue(withIdentifier: &quot;BackToOverview&quot;, sender: self)
      }
      
      // Score Stepper
      @IBAction func ScoreStepperPressed(_ sender: UIStepper) {
          ScoreResultLabel.text = Int(sender.value).description
      }
      
      // Fairway Switch
      @IBAction func FairwaySwitchPressed(_ sender: UISwitch) {
          if FairwaySwitch.isOn {
              CurrentFairwayHits = 1
              FairwayResultLabel.text = &quot;X&quot;
          } else {
              CurrentFairwayHits = 0
              FairwayResultLabel.text = &quot;-&quot;
          }
      }
      
      // Green Switch
      @IBAction func GreenSwitchPressed(_ sender: UISwitch) {
          if GreenSwitch.isOn {
              CurrentGreenInRegulations = 1
              GreenResultLabel.text = &quot;X&quot;
          } else {
              CurrentGreenInRegulations = 0
              GreenResultLabel.text = &quot;-&quot;
          }
      }
      
  
      // Putt Stepper
      @IBAction func PuttStepperPressed(_ sender: UIStepper) {
          PuttResultLabel.text = Int(sender.value).description
      }
      
      
      // Save Button
      @IBAction func SaveButtonPressed(_ sender: UIButton) {
          if Int(PuttResultLabel.text!)! &gt;= Int(ScoreResultLabel.text!)! {
              showAlert()
          } else {
          score = score + Int(ScoreResultLabel.text!)!
          fairwayhits = fairwayhits + CurrentFairwayHits
          greeninregulation = greeninregulation + CurrentGreenInRegulations
          putts = putts + Int(PuttResultLabel.text!)!
          
          print(score)
          print(fairwayhits)
          print(greeninregulation)
          print(putts)
              
          performSegue(withIdentifier: &quot;ShowResult&quot;, sender: self)
          }
      }
      
      
      func showAlert() {
          let alert = UIAlertController(title: &quot;Ops, something went wrong&quot;, message: &quot;The number of putts must be smaller than your score!&quot;, preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: &quot;Ok&quot;, style: .cancel, handler: { action in print(&quot;tapped Dismissed&quot;)}))
          
          present(alert, animated: true)
      }
  
      
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if SaveButton.isTouchInside,
             let resultVC = segue.destination as? ResultViewController
              {
  
              CurrentScore = score /// get the current cell's text
              CurrentPutts = putts
              resultVC.score = resultVC.score + String((resultVC.ScoreResultLabel.text!))
              resultVC.fairway = String(fairwayhits)
              resultVC.green = String(greeninregulation)
              resultVC.putt = String(putts)
              
              
                 }
      }
      
      
  
      override func viewDidLoad() {
          
          super.viewDidLoad()
  
          ScoreStepper.wraps = true
          ScoreStepper.autorepeat = true
          ScoreStepper.minimumValue = 1
          ScoreStepper.maximumValue = 20
          
          PuttStepper.wraps = true
          PuttStepper.autorepeat = true
          PuttStepper.minimumValue = 0
          PuttStepper.maximumValue = 20
          
  
          TitleLabel.text = text
      
      }
  }
  </code></pre>
  <p>And the Result view Controller:</p>
  <pre><code>
  
  class ResultViewController: UIViewController {
      
      var score = &quot;&quot;
      var fairway = &quot;&quot;
      var green = &quot;&quot;
      var putt = &quot;&quot;
      
  
      @IBOutlet weak var TitleLabel: UILabel!
      @IBOutlet weak var BackButton: UIButton!
      @IBOutlet weak var ShareButton: UIButton!
      
      // Score Label
      @IBOutlet weak var ScoreLabel: UILabel!
      @IBOutlet weak var ScoreResultLabel: UILabel!
      @IBOutlet weak var ScoreFrameLabel: UILabel!
      
      // Fairway Label
      @IBOutlet weak var FairwayLabel: UILabel!
      @IBOutlet weak var FairwayResultLabel: UILabel!
      @IBOutlet weak var FairwayFrameLabel: UILabel!
      
      // Green Label
      @IBOutlet weak var GreenLabel: UILabel!
      @IBOutlet weak var GreenResultLabel: UILabel!
      @IBOutlet weak var GreenFrameLabel: UILabel!
      
      // Putt Label
      @IBOutlet weak var PuttLabel: UILabel!
      @IBOutlet weak var PuttResultLabel: UILabel!
      @IBOutlet weak var PuttFrameLabel: UILabel!
      
      // Back Button Action
      @IBAction func BackButtonPressed(_ sender: UIButton) {
          performSegue(withIdentifier: &quot;BackToOverview&quot;, sender: self)
          
      }
  
  
          
      override func viewDidLoad() {
          super.viewDidLoad()
  
          ScoreResultLabel.text = &quot;\(score)&quot;
          FairwayResultLabel.text = &quot;\(fairway)&quot;
          GreenResultLabel.text  = &quot;\(green)&quot;
          PuttResultLabel.text = &quot;\(putt)&quot;
          
          
      }
      
  
   
  
  }
  </code></pre>