I am passing data from a table view controller to a detail view. I tried using indexPath.row directly in my prepareForSegue method, however it displays an error
use of unresolved identifier 'indexPath'
So, after searching the Internet, I set the variable indexOfSelectedPerson , which is assigned the value indexPath.row . The problem when starting the application in the simulator is that prepareForSegue gets the initial value of indexOfSelectedPerson (0), and then only gets the value of the selected row after I click on it. Therefore, when I click the "Back" button in the simulator and select a different line, the detailed view displays information about the line I selected the previous time.
import UIKit class MasterTableViewController: UITableViewController { var people = [] var indexOfSelectedPerson = 0 override func viewDidLoad() { super.viewDidLoad() people = ["Bob", "Doug", "Jill"] } override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { return 1 } override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { return people.count } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> 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])" } } } }
So, choosing Doug when the application first starts in the simulator, you will see the details for Bob, because indexPathOfSelectedPerson is 0. Click the back button, and then select “Jill” to display the details for Doug, because indexPathOfSelectedPerson 1 when I pressed Doug the previous time. I assume the problem is with the order in which the methods are called.
ios8 swift xcode6 segue
Shades
source share