简体   繁体   中英

Tableview to another viewcontroller

I have a controller named as "firstViewcontroller" where i have a UITableView named as "discoveredInstanceTableView". I want to load that UITableView in to another UIViewController named as "secondViewcontroller"

I have used the below code but it is not working, It says property "discoveredInstanceTableView" not found ...Anybody please help me:

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? it simply a ScrollableView which display content over its cells.

Eventually would recommend you read about TableView

EDIT: From the Above Comments

IMPORTANT: You should not embed UIWebView or UITableView objects in UIScrollView objects. 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.

Possible Alternatives of displaying TableView inside the SecondViewController

  • 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

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

  • Add delegate and data source (methods which responds when something interesting happened eg Cell going to populate called cellForRowAtIndexPath ) to current SecondViewController

  • 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.

I hope it may helps you!!!

declare in viewcontroller2

@property (nonatomic, strong) UITableView *table;

create table in 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 *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

[self.view addSubview:self.table];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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