Refresh table from another UIViewController

I have two view controllers, one of which ( ViewController ) has a table called tableView .

I would like to update this table from my other view controller ( pageView ).

I tried this in a pageView :

 ViewController*refresh; [refresh.tableView reloadData]; 

But that does not work.

The connecting segment between two view controllers is a push segment

What should I do? Should I do this through the storyboard?

+7
ios objective-c uitableview uiviewcontroller
source share
16 answers

Option 1

@ Class2

 @property (nonatomic) BOOL shouldRefresh; // in .h file - (void)viewWillAppear:(BOOL)animated // in .m file { [super viewWillAppear:animated]; if (_shouldRefresh) [self.tableView reloadData]; } 

@ Class1

 // Add this in any method where you want it to refresh and push to view ClassTwoController *viewController = [[ClassTwoController alloc] init]; [viewController setShouldRefresh:YES]; [self.navigationController pushViewController:viewController animated:YES]; 

* UPDATE:

Option 2

@Class 2

 // Add this line in viewDidLoad in same class you want the tableView to refresh [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableWithNotification:) name:@"RefreshTable" object:nil]; // Add this method just beneath viewDidLoad: - (void)refreshTableWithNotification:(NSNotification *)notification { [self.tableView reloadData]; } 

@ Class1

 // Call this when ever you want to refresh the tableView in Class2 [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil]; 
+18
source share

/ * Add this line to the viewDidLoad method in the ViewController (using this line you add a notification observer to the ViewController class) * /

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

/ * Add this method to the ViewController class (this method is called when a notification is sent to pageView) * /

 - (void)reloadTableview:(NSNotification *)notif { [tableView reloadData]; } 

/ * add this line when / where you want to update (this is a line notification message that was added to the ViewController) * /

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTableview" object:nil]; 
+3
source share

In ViewController1:

 > Create property for UITableview with nonatomic and retain > Now Create table Object. 

In ViewController2.m:

 ViewController1 *objV1 = [ViewController1 alloc]initWithNibName....]; [objV1.yourTableView reloadData]; 
+2
source share

If the second view controller is already loaded, you can use NSNotification in the firstviewcontroller. This notification will call the method in the second view controller. In this method, write code to reload the table.

+2
source share

If I understand correctly, you want to reload the table view that exists in the previous view controller of your navigation stack. If so, you can access it through the property of the navigation controller viewControllers .

 NSUInteger indexOfTableController = self.navigationController.viewControllers.count - 2; ViewController *tableController = (ViewController*)[self.navigationController.viewControllers objectAtIndex:indexOfTableController]; [tableController.tableView reloadData]; 
+1
source share

Most often this happens because you do not initialize the link to your second controller correctly, and it remains zero. There must be something like this.

 @implementation { ViewController *refresh; } - (void) openNewController { refresh = [[ViewController alloc] init]; [self.navigationController pushViewController:refresh animated:true]; } - (void) refreshNewController { [refresh.tableView reloadData]; } @end 
+1
source share

Good for example, you can call the previous ViewController VC1 and the current ViewController VC2.

So, you are doing a push segment from VC1 to VC2. You can do the following: pass the TableView link to VC2 during segue. Thus, you must create the public variable tblView UITableView in VC2, and then implement the prepareForSegue method in VC1

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"Segue_Identifier_From_StoryBoard"]) { // get your destination view controller VC2 *vc2 = [segue destinationViewController]; vc2.tblView = TableView; } } 

Then in VC2 you can just call [tblView reloadData];

Just make sure you set the tblView link after you finish working with VC2.

+1
source share

You can check the address of viewcontroller.tableview and refresh.tableView, and I think they will not be the same; you can make singleton for the viewcontroller and then call for example

 viewcontroller *vc = [viewcontroller sharedInstance]; [[vc tableview] reloadData]; 
+1
source share

Create a method in your pageView

 -(void) reloadVCController{ ViewController*vc=[[ViewController alloc]init]]; [vc.tableview reloadData; } 

And use it where you want to call it

 [self reloadVCController]; 
+1
source share
 ViewController*refresh; [refresh.tableView reloadData]; 

When you see the above code, you are not assigning a ViewController instance refresh . When reloadData() executed, your refresh will be zero, so obviously it will not respond to any action.

 ViewController*refresh = originalInstance; //assign your original instance here [refresh.tableView reloadData]; 

this will work, and the highlight that needs to be noted here is that your viewcontroller update should be the top-most instance on the stack, because any UI operation should happen in the main thread.

+1
source share

Use the NSnotification Center, add an observer to this view controller, and write a method for the reload table. [self.tableView reloadData]; And from the current viewing message, the observer is notified of this.

+1
source share

If you tried all the answers, make sure that before restarting the tableView, the dataSource is actually updated with new / additional / content.

+1
source share

Reloaddata needs to be done in the parentview controller because it calls the tableview delegate methods to reload the table, such as CellForRowAtIndexpath, and many others. This means that you need to define a public method in the parent controller that will contain the reloaddata command and call it from the child view controller.

public Method:

Parentviewcontroller.h

 @interface -(void) reloadTable:(id) sender; 

parentviewcontroller.m

 @implementation - (void) reloadTable:(id) sender { [self.tableview reloaddata]; return; } 

childviewcontroller.h

  #import parentviewcontroller.h @class parentviewController @interface childviewcontroller :UIViewController (or UITableViewController) @property (nonatomic, strong) parentViewController *parent; 

childviewController.m

 @implementation @synthesize parent; /* Note the parent instance is passed from the parent to the child view controller, it is not allocated and initialised in the child controller - many ways to do this depending on coding style */ /* after updating the data */ [parent reloadTable]; 
+1
source share

I think the problem is that you are not loading the controller view update . So the reloadTable message just goes to zero. You can force the view manager to load its view by calling view getter :

 [viewController view]; 

So, check that your tableView is zero, and if so, initialize it with the described method.

+1
source share

As a makeup response to emotionality:

If you want to skip the run animation, just set the bool property to your ClassTwoController to see if it loaded the first view of this view

ClassTwoController.h:

 @property (nonatomic, assign) BOOL notFirstTimeRun; 

ClassTwoController.m:

  -(void)viewWillAppear:(BOOL)animated // in .m file { if(self.notFirstTimeRun) { [self.tableView reloadData]; } else { [self playStartUpAnime]; } } -(void)playStartUpAnimation // in .m file { //Place your codes for animation here } 

in your ClassOneController .m initialize it with check bool:

 ClassTwoController *viewController = [[ClassTwoController alloc] init]; viewController.notFirstTimeRun = YES; 
+1
source share

If you have a parent-child relationship, you can add a weak property to the child viewcontroller, like this

 @property (nonatomic, weak) ParentViewController *parentController; 

Then, when you need to reload the table, you simply call [parentController.tableView reloadData]

Another way would be to use NSNotificationCenter. In the controller, you want to reload the table, you must subscribe to the notification ( [[NSNotificationCenter defaultCenter] addObserver...] ) and implement the selector that you provide to this method. In another controller, you must send a notification with this name.

You can also just upgrade viewWillAppear if it suits you.

+1
source share

All Articles