不能使用来自另一个类的reloadData [英] Can't use reloadData from another class

查看:130
本文介绍了不能使用来自另一个类的reloadData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个类,classA和classB
在类A中,我有一个tableview根据需要工作和刷新。所有的委托和datadource都很好,还有一个属性 @property(nonatomic,retain)UITableView * myTableView;



我不想把 reloadData 放在classD的viewDidAppear或viewWillAppear中。
我想从classB中触发 [myTable reloadData]



感谢

解决方案

使用委托重新载入您的表格。 p>

在tableview所在的类中,在viewDidLoad中写下:

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@updateLeftTable
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkRes :) name:@updateLeftTableobject:nil];

然后在同一个类中实现它的功能:

   - (void)checkRes:(NSNotification *)notification 
{
if([[notification name] isEqualToString:@updateLeftTable])
{
[_rearTableView reloadData];
}
}

最后,在要重新加载的类中你的tableview
粘贴以下行(重要的是,这行在你要重载你的tableview的方法中):

  [[NSNotificationCenter defaultCenter] postNotificationName:@updateLeftTableobject:self]; 

告诉我如果您有任何问题。


I have 2 classes, classA and classB In classA I have a tableview that works and refreshes on demand. all the delegates and datadource are fine and there's also a property @property (nonatomic, retain) UITableView *myTableView;

I don't want to put reloadData in viewDidAppear or viewWillAppear of classA. I want to fire [myTable reloadData] from classB.

Thanks

解决方案

Use delegates to reload your tableview.

In the class where the tableview is, write this in viewDidLoad:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateLeftTable"
                                              object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(checkRes:) name:@"updateLeftTable" object:nil];

Then in the same class implement its function like so:

-(void)checkRes:(NSNotification *)notification
{
   if ([[notification name] isEqualToString:@"updateLeftTable"])
   {
      [_rearTableView reloadData];
   }
}

Finally, in the class from where you want to reload your tableview paste the following line (It's important that this line goes in the method from where you want to reload your tableview):

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLeftTable" object:self];

Tell me if you have any problems.

这篇关于不能使用来自另一个类的reloadData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆