Magic Bullet Dave
Magic Bullet Dave

Reputation: 9076

Reloading Data in a UITableView when pressing a UITabBarItem

I have a UITabBarController as part of my app delegate and I want to trap when the user touches a specific tab (the favourites) and force the table within it to reload the data.

What would be best practice in this instance?

I have added the UITabBarDelegate protocol to my app delegate and implemented the didSelectViewController method. So far so good. Within the method I get a viewController, so I can check its title, etc. to determine which tab is selected.

How can I then send a reloadData message to the UITableView in the viewController? I tried creating a method in my FavouritesViewController class and calling that but it does not work.

Example code:

#pragma mark UITabBarController delegate methods

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   // If the favourites tab is pressed then reload the data
   if (viewController.title = @"Favourites")
   {
      if ([viewController respondsToSelector:@selector(reloadFavourites:)]) 
      {
         [viewController reloadFavourites];
      }
   }
} 

Upvotes: 2

Views: 3163

Answers (2)

aryland
aryland

Reputation: 133

The following piece of code should refresh your table every single time the view appears.

-(void)viewWillAppear:(BOOL)animated
{
    [[self tableView] reloadData];
}

Upvotes: 1

TechZen
TechZen

Reputation: 64428

It sounds like you need to add a [UITableView reloadData] to the tableViewController's viewWillDisplay method. This will cause the table to reload every time it is displayed.

If you want to force a reload will the tableview is already displayed, then calling reload from the method you created in the OP should work.

Upvotes: 3

Related Questions