Question

Hi There im creating an app using table views in storyboards and so far i am able to populate the table i have it set to making 4 cells and push to the same view controller from each cell but what i want to be able to do is each cell takes me to a new view controller

Here is what i have so far if you could point me in the right direction in to how to be able to do this would be great

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"ShowSimpleDetails"]) {
    SimpleTutorialsViewController *detailViewController = [segue destinationViewController];

    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

    int row = [myIndexPath row];
    detailViewController.simpleDetailModal = @[_simpleTitle[row], _simpleDescription[row], _simpleImages[row]];
   }
}
Was it helpful?

Solution

You can do it in the storyboard or in code:

In Storyboard

You can draw a segue from a cell to a destination view controller and give that segue a unique identifier in the property inspector. You can use this to segue to multiple destinations by having multiple cell prototypes, with each prototype having its own segue.

In Code

To do it in code, you would draw a segue from your view controller (not the cell) to each destination and give each segue a unique identifier. Then in didSelectRowAtIndexPath, you would decide which destination you want based on the selected index path and perform the segue like

NSString *segueIdentifier = @"someIdentifier";//or whatever logic you need to determined the appropriate identifier
id sender = self;//or whatever object you want to be the sender
[self performSegueWithIdentifier:segueIdentifier sender:sender];

prepareForSegue

In prepareForSegue, you'd have a conditional block for each possible segue identifier:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"oneSegueIdentifier"]) {
        //preparation
    } else if ([[segue identifier] isEqualToString:@"anotherSegueIdentifier"]) {
        //other preparation
    }
}

OTHER TIPS

You also have to click on the TableViewController > TableView > Attributes Inspector> Prototype Cells = numberOfSeguqes you want to make.

Just do a ctrl+drag from prototype cell to different destination view controllers and assign different identifiers.

Say you have a prototype cell and four different destination view controllers. Follow below steps

  1. ctrl+ drag from prototype cell to First destination view controller and assign a identifier in attributes inspector say "cell2view1"
  2. ctrl+ drag from prototype cell to Second destination view controller and assign a identifier in attributes inspector say "cell2view2"
  3. ctrl+ drag from prototype cell to Third destination view controller and assign a identifier in attributes inspector say "cell2view3"
  4. ctrl+ drag from prototype cell to Fourth destination view controller and assign a identifier in attributes inspector say "cell2view4"

Your [segue destinationViewcontroller] will be automatically set according to your storyboard.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {

    if ([[segue identifier] isEqualToString:@"cell2view1"]) 
    {
        //Do things
    }
    else if ([[segue identifier] isEqualToString:@"cell2view2"])
    {

    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top