iphone sdk - 重新加载表格视图数据 - 从单独的类

发布于 2024-08-19 12:52:19 字数 324 浏览 4 评论 0原文

我想运行代码 [tableView reloadData],除了我想从一个单独的类调用它到我想要重新加载数据的视图控制器。

(注意。如果有比 reloadData 更有效的方法来重新加载 tableview ,插话)。

假设我要重新加载的视图是“RootViewController”,而我当前位于“DetailViewController”中,我需要做什么才能使其工作。

我现在最好的尝试是[RootViewController.tableView reloadData],但它不对。 (我收到错误:在 .token 之前预期有 ':'。

问候,@norskben

I'd like to run the code [tableView reloadData], except I want to call it from a seperate class to the view controller I want to reload the data in.

(Note. If there is something more effective to reload a tableview than reloadData, chime in).

Say the view I want to reload is 'RootViewController', and I am currently in 'DetailViewController', what do i need to do to make it work.

My best attempt right now is [RootViewController.tableView reloadData], but its not right.
(I get error: expected ':' before . token.

Regards, @norskben

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

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

发布评论

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

评论(2

迷离° 2024-08-26 12:52:19

您可以使用通知或协议。

使用通知:

在完成保存数据后和从方法返回之前发布通知。像这样的东西:

// 发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" 对象:nil];

在处理表的控制器中,实现

- (void) dataSaved:(NSNotification *)notification{

    [self.tableView reloadData];

}

并在其 viewDidLoad 方法中添加以下代码来注册通知:

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

最后,在 dealloc 方法中取消注册,添加

[[NSNotificationCenter defaultCenter] removeObserver:self];

使用协议:

开始使用您的回调创建一个协议以前的控制器可以使用。

@protocol dataSavedDelegate
-(void)dataSaved;
@end

完成保存数据后:

[(id< dataSavedDelegate >)object dataSaved];

现在,在之前的控制器中处理委托方法:在 dataSaved() 方法中重新加载表。

You can use notifications or a protocol.

Using notifications:

post a notification just after finishing saving the data and before returning from the method. Something like this:

// post notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];

In the controller handling the table, implement

- (void) dataSaved:(NSNotification *)notification{

    [self.tableView reloadData];

}

and in its viewDidLoad method add the following code to register for notifications:

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

finally, unregister in the dealloc method adding

[[NSNotificationCenter defaultCenter] removeObserver:self];

Using a protocol:

start creating a protocol with a callback that your previous controller can use.

@protocol dataSavedDelegate
-(void)dataSaved;
@end

once you finish saving your data:

[(id< dataSavedDelegate >)object dataSaved];

now, in your previous controller you handle the delegate method: in the dataSaved() method you reload your table.

甜中书 2024-08-26 12:52:19

您需要在 DetailViewController 中引用 RootViewController 实例。在 DetailViewController 中声明一个属性,当您实例化 DetailViewController (我假设是从 RootViewController )时,将其设置为类似的内容

// in RootViewController.m
detailController.rootController = self;
[self.navigationController pushViewController:...

然后您可以从详细控制器访问根控制器:

[self.rootController.tableView reloadData];

另一种方法是在需要时在 DetailViewController 中发布自定义 NSNotification RootController 重新加载并让 RootController 侦听通知。

另一种方法是仅当用户返回根控制器时才重新加载表(在 RootViewController 的 viewWillAppear: 方法中执行),为什么要重新加载甚至不在屏幕上的表呢?

You need a reference to your RootViewController instance in DetailViewController. Declare a property in DetailViewController and when you instantiate your DetailViewController (from the RootViewController, I assume), set it with something like

// in RootViewController.m
detailController.rootController = self;
[self.navigationController pushViewController:...

Then you can access the root controller from the detail controller:

[self.rootController.tableView reloadData];

Another way is to post a custom NSNotification in DetailViewController when you want the RootController to reload and have the RootController listen for the notification.

Another way is to reload the table only when the user goes back to the root controller (do it in RootViewController's viewWillAppear: method), for why reload a table that isn't even on screen?

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