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

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

问题描述

我正在尝试开发一个表视图控制器,其中行连接到多个 视图控制器(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.).

所以如果我点击任何一行,它应该打开 Intermediate View 并将适当的控制器放在一个公共位置,所有控制器的其余部分都是相同的.假设我点击了一个索引映射到 TableView 的行.当它打开中间控制器时,它应该将 tableview 放在公共容器中,这个 table view 应该来自所有其他 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.

使用类似的方式创建其他转场,并在不同情况下的 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];

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

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