angry_developer
angry_developer

Reputation: 215

My reloaddata call in not calling the delegate methods in UItableviewcontroller?

I have an application in which i am using a sidemenu controller like a facebook app.its basically a UITableViewController.when i am using [self.tableView reloaddata] in the view will appear methode for the first time its working fine.Because of some requirement i need to reload this table view from another view controller.so i did this:

 SideMenuViewController *second = [[SideMenuViewController alloc]init];
 [second viewWillappear:YES]

where i am reloading the data.But when i am doing this the reloaddata is not calling the cellforindexmethode of the tableviewcontroller But it is calling the numberOfRowsInSection methode.Can anybody help me in finding where i am going wrong.

Upvotes: 2

Views: 1318

Answers (5)

Marius Ciocanel
Marius Ciocanel

Reputation: 151

You could use a notification to to this.

In the SideMenuViewController viewDidLoad method you add a this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reloadTable)
                                             name:@"SideMenuShouldRefreshDataNotification"
                                           object:nil];

The reloadTable method looks like this:

-(void)reloadTable {
dispatch_async(dispatch_get_main_queue(),^{
  [self.tableView reloadData];
});
}

When you want to refresh the table you just post this notification from anywhere you want in your app.

[[NSNotificationCenter defaultCenter] postNotificationName:[NSNotification notificationWithName:@"SideMenuShouldRefreshDataNotification" object:nil]];

Upvotes: 3

Midhun MP
Midhun MP

Reputation: 107131

This code never reload your tableView:

SideMenuViewController *second = [[SideMenuViewController alloc]init];
[second viewWillappear:YES]

Because here you are creating a new instance of your SideMenuViewController class. If you call the reloadData it won't refresh the previous tableView instance.

And calling the viewWillappear delegate method forcibly is a bad habit.

Solution:

Declare an instance of SideMenuViewController in the @interface of your viewController and write propert for that. When you go to that view set the SideViewController instance. And when the user press the back button call:

[sideViewControllerObject.tablView reloadData];

Upvotes: 0

Vishal
Vishal

Reputation: 8256

Try like this and check this below:

[[self yourtableviewname] reloadData];

Upvotes: 0

Vjlakshmi
Vjlakshmi

Reputation: 80

Reload using a protocol delegate. Another view controller may not be able to call viewWillAppear in the manner you are using. REload the tableview by writing a delegate that would return to the controller which has tableview in it.

Hope it helps.

Upvotes: 0

Dave
Dave

Reputation: 1081

If you creates a new instance and call a method on it, it won't cause that your old one will be refreshed. Call refreshData on your existing TableView instance, instead of creating a new one.

Upvotes: 0

Related Questions