Search code examples
iosstoryboardrowuitableviewuistoryboardsegue

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.


Solution

  • 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;
    }