hellosheikh
hellosheikh

Reputation: 3015

IOS storyboard - show different views on selection of table cells

i am new to ios programming. I have multiple view controllers. i am showing the tableView Controller first and there is a navigation controller before it .. i want to show the specific view controller depending on the cell selected.. i have set tableView Controller to dynamic and prototype Cell to 1 and i have segue which connected from table to my one of view controller name APP1ViewController i have another two controllers name APP2 and APP3 .. how can i connect them so that on the specific cell specific view controller loads. here is the code of displaying cells .. at the moment there is one section and three rows in my table

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"AppCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    switch (indexPath.row)
    {
        case 0:
            cell.textLabel.text = @"APP 1 ";
            cell.detailTextLabel.text = @"Detail of App 1...";
            break;
        case 1:
            cell.textLabel.text = @"App2";
            cell.detailTextLabel.text = @"Detail of App 2 app...";
            break;
        case 2:
            cell.textLabel.text = @"App3";
            cell.detailTextLabel.text =
            @"Detail of App 3...";
            break;

        default:
            cell.textLabel.text = @"Unkown";
            cell.detailTextLabel.text = @"Unknown";
            break;
    }

    return cell;
}

Upvotes: 3

Views: 6990

Answers (3)

Hemant Chittora
Hemant Chittora

Reputation: 3162

Try this,

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
    UIViewController *viewController = nil;      
    switch (indexPath.row) {
    case 0:
       viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewController1"];
        break;
    case 1:
        viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewController2"];
        break;
    case 2:
        viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewController3"];
        break;

    default:
        viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewController4"];
        break;
     }
    [[self navigationController] pushViewController:viewController animated:YES];
    }

Upvotes: 5

ldindu
ldindu

Reputation: 4380

Implement One of Tablview's delegate method

tableView:didSelectRowAtIndexPath:

and on the above delegate method you can fetch particular view controller form your storyboard and instantiate and push to your navigation's controller as follows

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"Your Storyboard's identifier that you want to push"];
[[self navigationController] pushViewController:viewController animated:YES];

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

Create your three segues from the controller that has the table rather than from the table or the cell itself. Give each segue a unique identifier. In tableView:didSelectRowAtIndexPath:, call the table view controller's performSegueWithIdentifier:sender: method and select the identifier that matches the chosen row.

Upvotes: 7

Related Questions