开发者

Loading tableview from another class in iPhone

I have a RootViewController class and a UserSettingsController class. I have defined the UITableView methods (numberOfRowsInSection, cellForRowAtIndexPath) in the RootViewController class.

I want to reloa开发者_开发问答d the table view defined in the RootViewController class from the UserSettingsController class. How can I get control of the tableView Object in the RootViewController class from the UserSettingsController class?

I tried the following, but it tries to load a new tableview object.

RootViewController *rootViewController = [[RootViewController alloc]init]; 
[rootViewController.mytableView reloadData];
[rootViewController autorelease];


You can reload rootViewController.mytableView in viewWillAppear method of RootViewController itself. This will make rootViewController.mytableView reload when you are about to go to the rootViewController view. If the data you want to load is not much (as in takes more time to load like fetching data from the web) you will be fine with this solution.

Otherwise, to load rootViewController.mytableView from you settings view, you can use NSNotification like this:

In RootViewController.m :

//This goes in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewData) name:@"ReloadRootViewControllerTable" object:nil];

Then make a method like this:
-(void) reloadTableViewData{

   [mytableView reloadData];
}

In Settings view, where you want to reload the RootViewController tableView, write this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadRootViewControllerTable" object:nil];

This will automatically call the reloadTableViewData method of RootViewController without your need to do subclassing or anything. :)

Make use of notifications with custom name to call static methods in other classes. They are very handy.


I am presuming if what you want to do is to show the same exact table in two different view controllers.

Make myTableView a global or singleton and share it as a property between rootViewController and userSettingsController. Better yet, do a little more work and create a class for your myTableView so you can more easily set it up and manipulate it without having to spread duplicate code in the view controllers' implementation.

In each view controller, test to see if (myTableVIew == nil), and if so, go ahead and initialize and set it up (preferably by going through the tableview class you made). Once set up, you need to make sure you add myTableView to the properties (retained) of both view controllers. Then to display this tableView in each of the controllers, you will just have to do a [self.view addSubview:myTableView]; where self is the view controller that is active at the time.


Yes mahboudz you are absolutely correctly saying that tableview valuw ould be nil

Because if reinitialize the class from another class using init , the objects are reset to nil which are previously set.

And than when you want to reload it it doesn't works because the value of tableview is nil

I didnot get better answer for resolving this please let me know if any buddy knows the solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜