Adami
Adami

Reputation: 335

How to link a UITableViewController to multiple ViewControllers and how to call them

I'm trying to link cells from an UITableViewController to UIViewController, but each cell has to call a different UIViewController (so I imagine that I'll have to create one link to each view controller) and I don't know how to link them through storyboard.

This is what I have until now:

MainView.h

@interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *tableData;
}
@property (nonatomic, retain) NSArray *tableData;
@end

MainView.m

- (void)viewDidLoad
{
    tableData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
    [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *anotherVC = nil;
    if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
    {
        if (indexPath.row == 0)
        {
            anotherVC = [[ViewForFirstRow alloc] init];
        }
    NSLog(@"anotherVC %@", anotherVC); // shows that anotherVC = ViewForFirstRow
    [anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
    [anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self.navigationController pushViewController:anotherVC animated:YES];
    }
}

@end

So, when first row is selected and I didn't link UITableViewController to UIViewController (ViewForFirstRow), it shows a black screen.

If I link UITableViewController to ViewForFirstRow, it opens the view but without the effect I used in code [self.navigationController pushViewController:anotherVC animated:YES];, so it appears that it called the view due to the link and not because of the code [self.navigationController pushViewController:anotherVC animated:YES];

How can I call the view by code properly and what kind of link should I use in storyboard for that? Also, how can I link the UITableViewController to multiples UIViewController (ViewForFirstRow, ViewForSecondRow, ViewForThirdRow, etc).

Thanks!

Upvotes: 1

Views: 1204

Answers (2)

rdelmar
rdelmar

Reputation: 104082

First, you can't hook up segues from cell (in a dynamic table view) in the storyboard, you have to do it in code. The code should look like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0) {
        UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:@"anotherVC"];
        [self.navigationController pushViewController:anotherVC animated:YES];
    }else if (indexPath.row == 1) {
        UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:@"someOtherVC"];
        [self.navigationController pushViewController:someOtherVC animated:YES]; 
    } 
}

You shouldn't be doing anything with indexPath.section since your table view only has one section.

Upvotes: 2

Vishal
Vishal

Reputation: 8256

In storyboard presenting another viewcontroller in modally manner then use this:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
  Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Synergy"];
 [self presentModalViewController:objSynergy animated:YES];

Synergy is my controller name You can put here your controller name that you want to present.

Make changes with your requirement. And for different viewcontroller open from different row

Use this:

       if (indexPath.section==0) 
          {
              if (indexPath.row ==0) 
              {
              }
          }

Upvotes: 1

Related Questions