Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table View Controller each row connected to different view controller

I am trying to develop a Table View Controller,where rows are connected to multiple View Controllers (TextField,TextView,TableView,DatePicker,ImageView etc.).

So if I click on any row,it should open the Intermediate View and place the appropriate controller in a common place where rest of the thing will be same for all controller.Suppose I clicked on a row where the index is mapped to TableView.When It will open the Intermediate Controller, It should placed the tableview in the common container,this table view should come from a single TableView controller for all other Tableview.

I am new in ios and not able to design this.

What is the best way to design this? How do I implement this?

Root Table View

enter image description here

Thanks

like image 642
Nullify Avatar asked Jan 05 '15 08:01

Nullify


1 Answers

I would suggest that don't create cell in Storyboard and connect it. Instead leave empty table in storyboard and create cell using code. You can create custom cell by subclassing UITableViewCell.

In storyboard you just link table view with all view controller using segue and give it proper identifier name.

enter image description here

Now implement all delegates methods of UITableView. Override -tableView:didSelectRowAtIndexPath: method and on row selection perform segue for specific row.

Example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"BasicCoreDataSegue" sender:self];
            break;

        default:
            break;
    }
}

Here in above case if you select first row it will push view controller which is connect with BasicCoreDataSegue segue in Storyboard, you can compare it with image.

Using similar way create other segues and call them in didSelectRowAtIndexPath method in different switch case.

Also If you want to pass any values to push controller, override below method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([[segue identifier] isEqualToString:@"BasicCoreDataSegue"]) {
        // Get reference to the destination view controller
        TextViewController *vc = [segue destinationViewController];
        vc.textView.text = "Hello";
    }
} 

Edit:

Above code works for common controller. Now you don't need to create more segues also in didSelectRowAtIndexPath method set Intermediate controller segue.

Use [self.tableView indexPathForSelectedRow] method to get selected row in prepareForSegue method.

For Example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self performSegueWithIdentifier:@"BasicCoreDataSegue" sender:self];
}

Now when prepareForSegue is called then set integer value for Intermediate controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].

        // You can get selected row using below line
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        // Pass the selected object to the new view controller.
        if ([[segue identifier] isEqualToString:@"BasicCoreDataSegue"]) {
            // Get reference to the destination view controller
            IntermediateController *vc = [segue destinationViewController];
            vc.selectedIndex = indexPath.row;
        }
    }

In above code selectedIndex is a integer variable which is used to track that which row is selected.

Now in Intermediate controller in -viewDidLoad() use switch case to get controller object which you want from row selection and add its view as a subview in Intermediate Controller.

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

TextViewController *controller = (TextViewController*)[storyBoard 
                    instantiateViewControllerWithIdentifier: @"TextViewControllerId"];

[self.topView addSubview:controller.view];
like image 80
Kampai Avatar answered Sep 21 '22 04:09

Kampai