iphone sdk-重新加载tableview数据-来自单独的类 [英] iphone sdk - reload tableview data - from a separate class
问题描述
我想运行代码[tableView reloadData],除了我想从一个单独的类中调用它,然后再将其加载到视图控制器中.
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.
(注意.如果有比reloadData更有效的方法来重新加载表格视图,请输入提示音.)
(Note. If there is something more effective to reload a tableview than reloadData, chime in).
说我要重新加载的视图是'RootViewController',而我目前在'DetailViewController'中,我需要做些什么才能使其正常工作.
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.
我目前最好的尝试是[RootViewController.tableView reloadData],但它不正确. (我收到错误:令牌前应有':'.
My best attempt right now is [RootViewController.tableView reloadData], but its not right. (I get error: expected ':' before . token.
致谢,@ norskben
Regards, @norskben
推荐答案
您可以使用通知或协议.
You can use notifications or a protocol.
使用通知:
在完成保存数据之后以及从方法返回之前发布通知.像这样:
post a notification just after finishing saving the data and before returning from the method. Something like this:
//发布通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved"对象:无];
// post notification [[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];
在处理表格的控制器中,实施
In the controller handling the table, implement
- (void) dataSaved:(NSNotification *)notification{
[self.tableView reloadData];
}
并在其viewDidLoad
方法中添加以下代码以注册通知:
and in its viewDidLoad
method add the following code to register for notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dataSaved:)
name:@"DataSaved" object:nil];
最后,在添加的dealloc方法中取消注册
finally, unregister in the dealloc method adding
[[NSNotificationCenter defaultCenter] removeObserver:self];
使用协议:
使用先前的控制器可以使用的回调开始创建协议.
start creating a protocol with a callback that your previous controller can use.
@protocol dataSavedDelegate
-(void)dataSaved;
@end
完成数据保存后:
[(id< dataSavedDelegate >)object dataSaved];
现在,在您以前的控制器中,您可以处理委托方法:在dataSaved()
方法中,您将重新加载表.
now, in your previous controller you handle the delegate method: in the dataSaved()
method you reload your table.
这篇关于iphone sdk-重新加载tableview数据-来自单独的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!