Search code examples
xcodeuitableviewsegue

Passing an index between tableviews using prepareForSegue not working


What I'm trying to do:

When a category is tapped in tableViewController 1 the corresponding information is displayed in tableViewController 2

How I am trying to do this:

I have a property var index = 0 in tableViewController 1. index is equal to the indexpath.row

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    index = indexPath.row
    print("Button tapped\(index)")
}

I have a prepareForSegue that passes index to tableViewController 2. tableViewController 2 also has a property named var index = 0. I am making subjectDetailTableViewController.index (tableViewController 2's index) = tableViewController 1's index

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let subjectDetailTableViewController = segue.destination as? SubjectDetailsTableViewController {
        // make the indexes equal between view controllers
        subjectDetailTableViewController.index = index
    }
}

Next, the viewWillAppear method of tableViewController2 uses the new value of index to determine how to load the cells

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Determine the subject tapped, then correspond with the correct information

    if index == 0 {
        loadAnatomy()
    } else
        if index == 1 {
            loadFormulas()
        } else
            if index == 2 {
                loadHemodynamics()
            } else
                if index == 3 {
                    loadProcedures()
                } else
                    if index == 4 {
                        loadRadiology()
    }
}

This way if the user taps the cell called ANATOMY (which is indexPath.row number 0) in tableViewController1 the loadAnatomy() method will populate tableViewController 2's cells with ANATOMY information

The problem:

It does not always work. It seems to be one step behind. If I tap another category i.e. FORMULAS (which is indexPath.row number 1) it will load Anatomy. Then if I go back to tableViewController 1 and press FORMULAS for a second time it works correctly.

I thought I had the if statement in the wrong spot, so I moved it from the viewDidLoad method of tableViewController 2 to the viewWillAppear method. But that did not fix this.

What am I missing? Thank you in advance.


Solution

  • I updated my override function from:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let subjectDetailTableViewController = segue.destination as? SubjectDetailsTableViewController {
        // make the indexes equal between view controllers
        subjectDetailTableViewController.index = index
    }
    

    }

    To:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        super.prepare(for: segue, sender: sender)
    
        let indexPath = tableView.indexPathForSelectedRow
    
        let index = indexPath?.row
    
        if let subjectDetailTableViewController = segue.destination as? SubjectDetailsTableViewController {
            // make the indexes equal between view controllers
            subjectDetailTableViewController.index = index!
        }
    
        if let mainViewController = segue.destination as? MainViewController {
            // Do not animate launch when seguing back to Main Menu
            mainViewController.initialLaunch = false
        }
    
    }