Table View Controller每行连接到不同的视图控制器 [英] Table View Controller each row connected to different view controller

查看:85
本文介绍了Table View Controller每行连接到不同的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个表视图控制器,其中行连接到多个视图控制器(TextField,TextView,TableView,DatePicker,ImageView等)。

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

因此,如果我点击任何一行,它应该打开中间视图并将相应的控制器放在一个公共场所其余部分对于所有控制器都是相同的。假设我点击了一个索引被映射到TableView的行。当它打开中间控制器时,它应该将tableview放在公共容器中,这个表视图应该来自一个所有其他Tableview的单个TableView控制器。

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.

我是ios的新手而无法设计它。

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

设计这个的最佳方法是什么?我该如何实现?

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

谢谢

推荐答案

我建议不要在Storyboard中创建单元格并连接它。而是将空表留在故事板中并使用代码创建单元格。您可以通过继承 UITableViewCell 来创建自定义单元格。

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.

在故事板中,您只需使用segue将表视图链接到所有视图控制器,并为其指定正确的标识符名称

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

现在实现 UITableView 的所有委托方法。覆盖 -tableView:didSelectRowAtIndexPath:方法和行选择为特定行执行segue。

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

示例:

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

        default:
            break;
    }
}

在上面的情况下如果选择第一行,它将会推送视图控制器,它与Storyboard中的 BasicCoreDataSegue segue连接,你可以将它与图像进行比较。

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.

使用类似方式创建其他segues并在不同切换情况下在 didSelectRowAtIndexPath 方法中调用它们。

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



编辑:



以上代码适用于通用控制器。现在你也不需要在 didSelectRowAtIndexPath 方法设置中间控制器segue中创建更多segue。

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

使用 [self.tableView indexPathForSelectedRow] 获取 prepareForSegue 方法中所选行的方法。

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

例如:

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

现在 prepareForSegue 然后为中级控制器调用然后设置整数值。

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

以上代码 selectedIndex 是一个整数变量,用于跟踪选择哪一行。

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

现在在 -viewDidLoad中的中间控制器中)使用switch case从行选择中获取所需的控制器对象,并将其视图作为子视图添加到中间控制器中。

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];

这篇关于Table View Controller每行连接到不同的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆