简体   繁体   English

Tableview 到另一个视图控制器

[英]Tableview to another viewcontroller

I have a controller named as "firstViewcontroller" where i have a UITableView named as "discoveredInstanceTableView".我有一个名为“firstViewcontroller”的控制器,其中我有一个名为“discoveredInstanceTableView”的UITableView I want to load that UITableView in to another UIViewController named as "secondViewcontroller"我想将该UITableView加载到另一个名为“secondViewcontroller”的UIViewController

I have used the below code but it is not working, It says property "discoveredInstanceTableView" not found ...Anybody please help me:我使用了下面的代码,但它不起作用,它说找不到属性“discoveredInstanceTableView”......任何人请帮助我:

In the firstViewcontroller:在第一个视图控制器中:

  IBOutlet UITableView *discoveredInstanceTableView;

In the Secondviewcontroller:在第二个视图控制器中:

 firstViewcontroller *vc1 = [[firstViewcontroller alloc]initWithNibName:@"firstViewcontroller" bundle:nil];

    [self addChildViewController:vc1];
    [self.myTableview addSubview:vc1.discoveredInstanceTableView];

What you asked is valid only if you curious to know why the above thing is not working answer would be仅当您想知道为什么上述内容不起作用时,您所问的内容才有效答案是

You are doing something that is not allowed, this can not be done as per the documentation.您正在做一些不允许的事情,根据文档,这是无法完成的。 However, If we forget about the right wrong approach, you probably adding a table view as a subview over a table view itself and I am sure you passing a table view to a table view which might not be allocated.但是,如果我们忘记了正确的错误方法,您可能会添加一个表视图作为表视图本身的子视图,并且我确定您将表视图传递给可能未分配的表视图。

First think about the UITableView how it works?首先想想UITableView是怎么工作的? it simply a ScrollableView which display content over its cells.它只是一个在其单元格上显示内容的 ScrollableView。

Eventually would recommend you read about TableView最终会建议您阅读有关TableView 的信息

EDIT: From the Above Comments编辑:从上面的评论

IMPORTANT: You should not embed UIWebView or UITableView objects in UIScrollView objects.重要提示:您不应在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。 If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.»如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆并错误处理。» As UITableView is a UIScrollView, this applies here as well.由于 UITableView 是一个 UIScrollView,这也适用于这里。

Possible Alternatives of displaying TableView inside the SecondViewController在 SecondViewController 中显示 TableView 的可能替代方法

  • Use @Rajath Kornaya's Answer And In my opinion that is not right approach since whenever you required callback action like on cell tap, you want to display an alert(or something else), you can't get the delegate callback inside the SecondViewController使用@Rajath Kornaya 的答案 在我看来,这不是正确的方法,因为每当您需要像点击单元格这样的回调操作时,您想要显示警报(或其他内容),您都无法在 SecondViewController 中获得委托回调

But there are so many other right approaches available, that you should follow up.但是还有很多其他正确的方法可用,您应该跟进。

  • Create a TableView separately either programmatically or through the XIB/Storyboard以编程方式或通过 XIB/故事板单独创建一个 TableView

  • Add delegate and data source (methods which responds when something interesting happened eg Cell going to populate called cellForRowAtIndexPath ) to current SecondViewController向当前 SecondViewController 添加委托和数据源(当发生有趣的事情时响应的方法,例如 Cell 将填充称为cellForRowAtIndexPath

  • Define all required data source methods and write proper code.定义所有必需的数据源方法并编写适当的代码。

  • If you required to do something on cell tap, add specific delegate method too.如果您需要在单元格点击上执行某些操作,请添加特定的委托方法。

But if you want to reuse the FirstViewController Class TableView simply create a CustomView and add TableView inside there and simply add that view to each view controller class.但是,如果您想重用 FirstViewController 类 TableView,只需创建一个 CustomView 并在其中添加 TableView,然后将该视图添加到每个视图控制器类中即可。

I hope it may helps you!!!希望可以帮到你!!!

declare in viewcontroller2在 viewcontroller2 中声明

@property (nonatomic, strong) UITableView *table;

create table in viewcontroller1在 viewcontroller1 中创建表

tableView=[[UITableView alloc]initWithFrame:CGRectMake(10, 10, 250, 300) style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.backgroundColor=[UIColor greenColor];

while calling viewcontroller2 pass table to viewcontroller2在调用 viewcontroller2 时将表传递给 viewcontroller2

  ViewController2 *v2=[[ViewController2 alloc]init];
  v2.table=tableView;
  UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:v2];
  [self presentViewController:navigation animated:YES completion:nil];

in viewcontroller2 access the table using the global variable在 viewcontroller2 中使用全局变量访问表

[self.view addSubview:self.table];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM