Cescy
Cescy

Reputation: 1991

UITableViewCell segues to different static UITableViewController based on row selected?

I have 3 cells in a UITableView. Person, Car and House and I want each one to segue to a custom and different STATIC TableViewController so the user can do some setup.

What is the most efficient way to go about this?

In prepareForSegue do I query and call the method didSelectRowAtIndexPath then do what? Or in the storyboards do I setup a push segue from one cell to a separate TVC, 3 times?

Does anyone know of any good tutorial? I dont know how to go about this thanks.

Upvotes: 0

Views: 905

Answers (3)

Nick
Nick

Reputation: 949

I am guessing you are using storyboards? Are the cells static cells, and always in the same order?

If so I am guessing you have a UIViewController or UITableViewController in the storyboard which is populated with Person, Car, and House.

You then have 3 different view controllers that you wish to navigate to depending on which of Person, Car or House is selected?

As you are using prototype cells, I would do the following:

As previously mentioned, when creating the cell do:

[cell setTag:indexPath.row];

then in didSelectRowAtIndexPath do the following:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

switch ([cell tag]) {
    case 0:
        PersonDetailViewController *personDetailViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"PersonDetails"];
        [[self navigationController] pushViewController:personDetailViewController animated:YES];
        break;
    case 1:
        // Do similar for Car
        breal;
    case 2:
        // Do similar for House

    default:
        break;
}

Upvotes: 1

Yohan
Yohan

Reputation: 1108

First Connect your main tableview with three individual TVC and add segue for each add identifier for that

then in didSelectRowAtIndexpath check which indexpath has been pressed and call that viewController like below

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

if(indexPath.row == 1)
{
   [self performSegueWithIdentifier:@"view1" sender:self];
}
else if(indexPath.row == 2)
{
   [self performSegueWithIdentifier:@"view2" sender:self];
}
else if(indexPath.row == 3)
{
   [self performSegueWithIdentifier:@"view3" sender:self];
}
}

Then in prepareforSegue find which one view is called

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"view1"]){

        TableView1 *tableMenu = segue.destinationViewController;

       }

 else if([[segue identifier] isEqualToString:@"view2"]){

        TableView2 *tableMenu2 = segue.destinationViewController;

       }
 else if([[segue identifier] isEqualToString:@"view3"]){

        TableView3 *tableMenu3 = segue.destinationViewController;

       }
}

Hope this works if any mistake someone correct me thnx

Upvotes: 0

Sudershan shastri
Sudershan shastri

Reputation: 720

You can do like this:

In your didSelectRowAtIndexPath method, do this:

if(indexPath.row == 1)
{
   // push to controller 1
}
else if(indexPath.row == 2)
{
   // push to controller 2
}
else if(indexPath.row == 3)
{
   // push to controller 3
}

Upvotes: 0

Related Questions