iphone sdk - 重新加载表格视图数据 - 从单独的类
我想运行代码 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用通知或协议。
使用通知:
在完成保存数据后和从方法返回之前发布通知。像这样的东西:
// 发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" 对象:nil];
在处理表的控制器中,实现
并在其
viewDidLoad
方法中添加以下代码来注册通知:最后,在 dealloc 方法中取消注册,添加
使用协议:
开始使用您的回调创建一个协议以前的控制器可以使用。
完成保存数据后:
现在,在之前的控制器中处理委托方法:在
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
and in its
viewDidLoad
method add the following code to register for notifications:finally, unregister in the dealloc method adding
Using a protocol:
start creating a protocol with a callback that your previous controller can use.
once you finish saving your data:
now, in your previous controller you handle the delegate method: in the
dataSaved()
method you reload your table.您需要在 DetailViewController 中引用 RootViewController 实例。在 DetailViewController 中声明一个属性,当您实例化 DetailViewController (我假设是从 RootViewController )时,将其设置为类似的内容
然后您可以从详细控制器访问根控制器:
另一种方法是在需要时在 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
Then you can access the root controller from the detail controller:
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?