从另一个视图返回后从表视图重新加载数据

发布于 2024-07-27 00:47:52 字数 1591 浏览 5 评论 0原文

我的应用程序有问题。 任何帮助将不胜感激。 基本上是从视图A到视图B,然后从视图B返回。

在视图A中,它具有从数据库加载的动态数据,并显示在表视图上。 在此页面中,它还有编辑按钮,但不在导航栏上。 当用户点击编辑按钮时,它会转到视图 B,其中显示选择视图。 用户可以在这里进行任何更改。 完成后,用户点击导航栏上的后退按钮,它将更改保存到 NSUserDefaults 中,通过弹出视图 B 返回到视图 A。

当返回到视图 A 时,它应该从UIUserDefaults,确实如此。 我使用 NSLog 打印到控制台,它显示了正确的数据。 它还应该调用 viewWillAppear: 方法来获取表视图的新数据,但它没有。 它甚至没有调用 tableView:numberOfRowsInSection: 方法。 我在这个方法中放置了一条 NSLog 语句,但没有在控制台中打印出来。

结果,视图A仍然有旧数据。 在视图 A 中获取新数据的唯一方法是停止并启动应用程序。

视图A和视图B都是UIViewController的子类,具有UITableViewDelegateUITableViewDataSource

这是我在视图 A 中的代码:

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"enter in Schedule2ViewController ...");

    // load in data from database, and store into NSArray object

    //[self.theTableView reloadData];
    [self.theTableView setNeedsDisplay];
    //[self.theTableView setNeedsLayout];
}

在这里,“theTableView”是一个 UITableView 变量。 我尝试了“reloadData”、“setNeedsDisplay”和“setNeedsLayout”的所有三种情况,但似乎不起作用。

在视图B中,这里是导航栏上后退按钮对应的方法。

- (void)viewDidLoad {
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePreference)];
    self.navigationItem.leftBarButtonItem = saveButton;
    [saveButton release];
}

- (IBAction) savePreference {
    NSLog(@"save preference.");

    // save data into the NSUSerDefaults

    [self.navigationController popViewControllerAnimated:YES];
}

我的做法正确吗? 或者有什么我错过的吗?

I have a problem in my application. Any help will be greatly appreciated. Basically it is from view A to view B, and then come back from view B.

In the view A, it has dynamic data loaded in from the database, and display on the table view. In this page, it also has the edit button, not on the navigation bar. When user tabs the edit button, it goes to the view B, which shows the pick view. And user can make any changes in here. Once that is done, user tabs the back button on the navigation bar, it saves the changes into the NSUserDefaults, goes back to the view A by pop the view B.

When coming back to the view A, it should get the new data from the UIUserDefaults, and it did. I used NSLog to print out to the console and it shows the correct data. Also it should invoke the viewWillAppear: method to get the new data for the table view, but it didn't. It even did not call the tableView:numberOfRowsInSection: method. I placed a NSLog statement inside this method but didn't print out in the console.

As the result, the view A still has the old data. the only way to get the new data in the view A is to stop and start the application.

Both view A and view B are the subclass of UIViewController, with UITableViewDelegate and UITableViewDataSource.

Here is my code in the view A :

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"enter in Schedule2ViewController ...");

    // load in data from database, and store into NSArray object

    //[self.theTableView reloadData];
    [self.theTableView setNeedsDisplay];
    //[self.theTableView setNeedsLayout];
}

In here, the "theTableView" is a UITableView variable. And I try all three cases of "reloadData", "setNeedsDisplay", and "setNeedsLayout", but didn't seem to work.

In the view B, here is the method corresponding to the back button on the navigation bar.

- (void)viewDidLoad {
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePreference)];
    self.navigationItem.leftBarButtonItem = saveButton;
    [saveButton release];
}

- (IBAction) savePreference {
    NSLog(@"save preference.");

    // save data into the NSUSerDefaults

    [self.navigationController popViewControllerAnimated:YES];
}

Am I doing in the right way? Or is there anything that I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-08-03 00:47:52

首次加载视图时,它会调用 viewDidLoad 方法。 如果您创建一个堆栈,向下钻取它(从 A 到 B),然后返回(B 到 A),则 viewDidLoad 不会在 A 上再次调用。您可能想要尝试的是将 A 传递到 B (通过传递 self )并调用 viewDidLoad 方法来获取新数据,然后调用 tableView 上的 reloadData 方法来重新填充表视图。

您可能想要尝试的是将数据获取和设置功能从 viewDidLoad 方法中取出,并将其放置在自己的 getData 方法中。 在 getData 方法的末尾,您可以放置​​一个 [self.tableView reloadData]; 来重置/重新填充表格视图。 从类 B 中,您可以调用 [self getData] 并最大限度地减少在类 B 中要做的工作量。这将有助于提高该代码的重用能力,并可能防止调用 viewDidLoad 产生的副作用方法。

When a view is first loaded, it calls the viewDidLoad method. If you create a stack, drill down into it (from A to B) and then return (B to A) the viewDidLoad does not get called again on A. What you may want to try is passing A into B (by passing in self) and call the viewDidLoad method to get the new data and then reloadData method on the the tableView to refill the table view.

What you may want to try is taking the data fetching and setting functionality out of the viewDidLoad method and place it in its own getData method. At the end of the getData method, you could place a [self.tableView reloadData]; to reset/refill the table view. From class B, you could call [self getData] and minimize the amount of work you would do in class B. This would help increase reuse-ability of that code and may prevent side effects from calling the viewDidLoad method.

诗化ㄋ丶相逢 2024-08-03 00:47:52

您还可以使用 viewDidAppear。 每次屏幕出现时都会调用它。 出于性能原因,请设置一个标志,以便您不会在第一个屏幕视图的 viewDidLoad 和 viewDidAppear 中重复相同的功能。

You could also use viewDidAppear. It is called every time the screen appears. For performance reasons, set a flag so you don't repeat the same functionality in viewDidLoad with viewDidAppear for the first screen view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文