How to push segue from cell click in table view to another view controller

12,960

Solution 1

First of all your UITableViewController must have a UINavigationController

To do it rapidly embedded it to your table from the xCode menu

enter image description here:

Then , you must create the segue, ctrl-drag from the UITableViewController to the destination viewController:

enter image description here

Then select the segue and give it an identifier in the property inspector:

enter image description here

Now you are able to perform this segue in code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    performSegueWithIdentifier("mySegue", sender: cell)
}

Solution 2

If you are not using UINavigationController than add a segue in storyboard from table cell to destination viewController and give a identifier to segue.

then on cell click call

- performSegueWithIdentifier:sender:

and implement

- prepareForSegue:sender:
Share:
12,960
user5513630
Author by

user5513630

Updated on June 05, 2022

Comments

  • user5513630
    user5513630 almost 2 years

    I have one table view controller and by clicking the cell i am redirecting the user to detail view controller.But when i perform segue " present view controller" its working fine.But what i need is?

    I need to perform push segue by clicking the table view cell.How to do that?

    Here is my code:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc: BusinessDetailViewController = storyboard.instantiateViewControllerWithIdentifier("BusinessDetailViewController") as! BusinessDetailViewController
            vc.BusinessData = arrDict[indexPath.row]
            self.presentViewController(vc, animated: true, completion: nil) 
        }
    

    I have tried this below line :

     self.navigationController?.pushViewController(vc, animated: true)
    

    But it doesn't work.Any Solution Please !!

  • user5513630
    user5513630 about 8 years
    in code.How to call my current view controoler with navigation controller.Beacuse there are more viewcontrollers.So if i do it now via storyboard.Sure my all views will get affect
  • Lucy
    Lucy over 6 years
    Thanks @jagdish this worked for me! I dragged from the Cell, selected in the outline view, to the View Controller, gave the segue an Identifier and then passed it in to performSegue(). Note that I'm not doing anything yet with the data in my example as its a Work in Progress to learn Swift. But the segue is working: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let row = indexPath.row let section = indexPath.section performSegue(withIdentifier: “MySegueID”, sender: self) }