无法使用其他类的reloadData

Seg*_*gev 2 cocoa cocoa-touch objective-c ios

我有2个类,classA和classB在classA中我有一个tableview可以按需工作和刷新.所有的代表和数据源都很好,还有一个属性@property (nonatomic, retain) UITableView *myTableView;

我不想放入reloadDataclassA的viewDidAppear或viewWillAppear.我想[myTable reloadData]从classB开火.

谢谢

aBi*_*l17 16

使用委托重新加载您的tableview.

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

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateLeftTable"
                                              object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(checkRes:) name:@"updateLeftTable" object:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLeftTable" object:self];
Run Code Online (Sandbox Code Playgroud)

如果您有任何问题,请告诉我.