How to make a push segue when a UITableViewCell is selected

| | August 18, 2015

I have a list of effects in a table view. I have created a top right bar button which does a push segue to another view controller which helps create a new effect. Now I want to add push segues to the table view cells so that the effects values maybe loaded on the add effects view controller and and I can save the edited values.

The question is can I create a push segue programmatically? If not can I pass the effect through a prepareforsegue? If I try to use a prepareforsegue, i run into a problem where control dragging from the UITableView does not let me create a push segue to the add effects view controller.

8 Responses to “How to make a push segue when a UITableViewCell is selected”

  1. Here is another option, that doesn’t require the use of didSelectRowAtIndexPath.

    You just wire up the segue in Interface Builder from the prototype cell to the destination.

    From there you can simply:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "AffiliationDetail", let destination = segue.destinationViewController as? AffiliateDetailViewController {
            if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) {
                var affiliation = affiliations[indexPath.row]
                destination.affiliation = affiliation
            }
        }
    }
    
  2. @meda’a answer was very helpful. Here is a Swift version that includes passing some data.

    First View Controller with TableView

    class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        var chosenCellIndex = 0
    
        // ...
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
            chosenCellIndex = indexPath.row
    
            // Start segue with index of cell clicked
            self.performSegueWithIdentifier("yourSegue", sender: self)
    
        }
    
        // This function is called before the segue
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            // get a reference to the second view controller
            let secondViewController = segue.destinationViewController as! SecondViewController
    
            // set a variable in the second view controller with the data to pass
            secondViewController.receivedCellIndex = chosenCellIndex
        }
    
    }
    

    Second View Controller

    class SecondViewController: UIViewController {
    
        @IBOutlet weak var label: UILabel!
    
        // This variable will hold the data being passed from the First View Controller
        var receivedCellIndex = 0
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            print(receivedCellIndex)
        }
    
    }
    

    The data passed was the cell index but it could have just as easily been some other value from the cell.

    Remember to make a segue in the storyboard and set an identifier for it.

    For a more basic example about passing data between View Controllers see this answer.

  3. control drag From View Controller to View Controller

    From View Controller to View Controlle!

    You will need to give an identifier to your segue:

    enter image description here

    Perform the segue:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"yourSegue" sender:self];
    }
    

    Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue deleguate:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Make sure your segue name in storyboard is the same as this line
        if ([[segue identifier] isEqualToString:@"yourSegue"])
        {
            //if you need to pass data to the next controller do it here
        }
    }
    
  4. You can do this by following methods:

    Firstly you need to import another view controller in current view.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

    inside above method you have to call a method
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
    like this:

    yourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    [self.navigationController YourViewController animated:YES];
    

    By this you can achieve push segue when UITableViewCell is selected..

    Hope this may help you…!

  5. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyViewController *myVController = (MyViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"StoryBordIdentifier"];
        [self.navigationController pushViewController:myVController animated:YES];
    }
    
  6. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"YourPushSegueName" sender:self];
    }
    
  7. You can use this method :

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    You can perform a function on selection of a particular tableview cell in this.

  8. You have 2 options: ctrl+drag from the prototype cell to the new vc and then you’ll segue from each cell and you have to implement prepare for segue.

    Second option, in didselectrowatindexpath you can instantiate your new viewcontroller using self.storyboard instantiateviewcontrollerwithidentifier…, then call pushviewcontroller or presentviewcontroller, you don’t need segues in storyboard or prepare for segue in this scenario.

Leave a Reply