1
votes

I am trying to connect one prototype cell to two different segue pathways that each lead to another view controller. I read in another posts that hooking up the segues has to be done from the source view controller itself to the destination view controller, not the prototype cell to the view controller.

My source view controller is called BuildsViewController, which is populated with tableview cells. I am working on an app where clicking the cell will open up the destination view controller based on whether the login or guest button was clicked at the root view controller. (buttonClicked is an instance variable that stores the button that the user clicked on the root view controller).

When I click on the cell on BuildsViewController, however, nothing happens. Where did I go wrong? Note that the login button has tag 0 and the guest button has tag 1.

     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch self.buttonClicked?.tag {
    case 0?:
            performSegue(withIdentifier: "showLogin", sender: self)
            break;
    case 1?:
            performSegue(withIdentifier: "showGuest", sender: self)
            break;

    default:
        preconditionFailure()
    }
}



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


    switch segue.identifier {
    case "showLogin"?:

       if let row = self.tableView.indexPathForSelectedRow?.row {

            let build = self.buildStore.allBuilds [row]
            let detailBuildsViewController = segue.destination as! BuildDetailViewController
                detailBuildsViewController.build = build
            break;
        }
    case "showGuest"?:


        if let row = self.tableView.indexPathForSelectedRow?.row {

            let build = self.buildStore.allBuilds [row]
                let detailBuildsViewControllerGuest = segue.destination as! BuildDetailViewControllerGuest
                detailBuildsViewControllerGuest.build = build
            break;
        }
    default:
        preconditionFailure("Unexpected segue identifier")
        }
    }
    override func tableView (_ tableView: UITableView,
                         cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
    let build = buildStore.allBuilds [indexPath.row]
    cell.textLabel?.text = build.name

    return cell

}

Thank you!

1
Show your cellForRowAt code - Zonily Jame
your didselect method is called?? - luckyShubhra
Oh that's right. I needed to override the didselect method and the method signature is wrong. I fixed it now. It works beautifully! Thanks! @luckyShubhra - S. Patel
You are welcome @S.Patel. SO ready to help :) - luckyShubhra

1 Answers

0
votes

Swift 3 modified the signature of didSelectRowAtIndexPath method. You need to add override to the method name to override the superclass method.

Replace:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

with

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