Reloading a table's data

梦想的初衷 提交于 2019-12-24 02:57:14

问题


I have 5 view controllers, which are in a navigation controller hierarchy. When I reach my last view controller, there's a button that lets me go back to the "Home" view controller (named CardWalletViewController) which is a table view controller. Here's the method I have in my last VC (PointsResultsVC)

- (IBAction)homePressed:(id)sender {
    NSArray *VCs = [self.navigationController viewControllers];
    [self.navigationController popToViewController:[VCs objectAtIndex:0] animated:YES];
}

CardWalletVC's cells is being filled up by values coming from the saved instances of a Card in NSUserDefaults and it works fine.

Now, what I want is to update the value in my CardWalletViewController, which is the points of a card coming from my PointsResultsVC. Note that this points is saved in NSUserDefaults.

Upon the process of trying to update of the value shown in my CardWalletVC, I placed [self.tableView reloadData]; inside -viewDidLoad, -viewWillAppear, and -viewDidAppear of the said class. I tried placing it one by one in each of this methods, yet it doesn't seem to work.

Advice Please.

EDIT: problem solved

This one served as my guide Save data from one tab and reloadData in another tab.

And as I have discovered, -viewDidLoad of a certain class will only be called once, all throughout runtime of app. Whereas -viewWillAppear, it will be called every time the view appears.

So, I just moved the way I am loading the values saved in NSUserDefaults from -viewDidLoad to -viewWillAppear. Also, inside -viewWillAppear I placed the [self.tableView reloadData]. Then there, problem solved.


回答1:


There's a lot of things that could be going wrong so you'll need to provide more info to narrow it down.

First of all, viewDidLoad will not be called so you can remove it from here. Both viewWillAppear and viewDidAppear will be called, so you only need it one of these 2 methods.

Next, you need to determine what the actual issue is.

1 - is self.tableView a proper reference to the table? Are you properly maintaining the reference?

2 - is the table actually reloading but using the old data, or is it not reloading at all? You can log the willDisplayCell method to see what is going on when the view appears.

With that info, the root cause can be narrowed down to a solvable problem.



来源:https://stackoverflow.com/questions/10527175/reloading-a-tables-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!