pass row number outside "did select" function

Can anyone please help me to pass information about row number selected in tableView? I need it in other view controller to choose data depending on selected row in previous table.

The Xcode blocks returning this data outside function. I cannot manage it for over a week. Thank you in advance.


This is my original code that I have written:


//to object search viewcontroller

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let rowNumber = indexPath.row

print(rowNumber) //just for testing

performSegue(withIdentifier: "ToObjectSearch", sender: self)

}


I have tried this:

//to object search viewcontroller

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> Int {

let rowNumber = indexPath.row

print(rowNumber)

performSegue(withIdentifier: "ToObjectSearch", sender: self)

return rowNumber

}


..but it completely blocks segue to next viewcontroller.

Accepted Reply

The way to pass data through a segue is using prepare(segue:)


Your desttination is a viewController class is DestViewController

Inside, define a property

var rowSelected : Int?


In the source controller, declare a var to hold the selection

fileprivate var selectedRow: Int?


that you set in didSelectRow

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        rowSelected = indexPath.row
        print(rowSelected)     //just for testing
        performSegue(withIdentifier: "ToObjectSearch", sender: self)
    }


   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the destination  view controller in destVC.
        // Pass the selected object to the new view controller.
        if let destVC = segue.destination as? DestViewController {
            destVC.rowSelected = rowSelected
        }
    }


Note: in the prepare, you should test it is the right segue, so that, if you define other vsegues you know which one to prepare


  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the destination  view controller in destVC.
        // Pass the selected object to the new view controller.
         if segue.identifier == "ToObjectSearch" 
             if let destVC = segue.destination as? DestViewController {
                 destVC.rowSelected = rowSelected
          }
        }
    }

Replies

The way to pass data through a segue is using prepare(segue:)


Your desttination is a viewController class is DestViewController

Inside, define a property

var rowSelected : Int?


In the source controller, declare a var to hold the selection

fileprivate var selectedRow: Int?


that you set in didSelectRow

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        rowSelected = indexPath.row
        print(rowSelected)     //just for testing
        performSegue(withIdentifier: "ToObjectSearch", sender: self)
    }


   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the destination  view controller in destVC.
        // Pass the selected object to the new view controller.
        if let destVC = segue.destination as? DestViewController {
            destVC.rowSelected = rowSelected
        }
    }


Note: in the prepare, you should test it is the right segue, so that, if you define other vsegues you know which one to prepare


  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the destination  view controller in destVC.
        // Pass the selected object to the new view controller.
         if segue.identifier == "ToObjectSearch" 
             if let destVC = segue.destination as? DestViewController {
                 destVC.rowSelected = rowSelected
          }
        }
    }

Thank You!

It works. I am quite fresh developer, I am learning. I have spent many hours to solve the problem and suddenly got idea - just ask somebody experienced.

Now I am going to my parents Sunday dinner feeling very goog! Thanks!

Great, enjoy dinner.


And don't forget to close the thread by marking the correct answer.


Edited

Take care if you have several sections in your tableView ; in this case, you should pass the complete indexPath (not only the row) to get the true cell reference in the destination view.

ok, thank You very much! I have pushed "correct answer" but probably it didn't work wor a while, something was wrong with logging, my answer to You also was able to be sent few hours after dinner 🙂

probably different time zone 😉 - I am in Poland 🙂