使用Objective-C从不同的ViewController重新加载TableView

时间:2016-07-20 11:42:26

标签: ios objective-c uitableview reload

我不确定如何实现这一点,因此我问这个问题。

我有2个名为abVCxyVC的ViewController(VC)。 abVC仅包含TableViewxyVC仅包含一个按钮。

当我点按xyVC上的按钮时,它应该在abVC内重新加载tableview,而不会移动到abVC

有可能吗?如果是..那么请帮助我,怎么做?

2 个答案:

答案 0 :(得分:4)

你可以同时使用阻止和NSNotification。

使用块可以按如下方式实现:

在xyzVC.h中创建如下属性:

@property(strong,nonatomic)void(^callBack)();

在xyzVC.m

中合成此属性
@synthesize callBack

在按钮点击事件

中调用此块
- (IBAction)btnClick:(UIButton *)sender {

if (callBack)
    {
        callBack();
    }

}

在你想要回电的abcVC中实现它:

[abVC setCallBack:^{
        //reload your tableview here
    }];

使用NSNotification,您可以按如下方式实施:

注册接收abcVC.m中的通知

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

- (void)reloadTableView{
  // Reload your tableview here
}

现在发布xyzVC的通知

- (IBAction)btnClick:(UIButton *)sender {

  [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:nil];

}

注意:如果您正在使用NSNotification,请不要忘记在取消视图控制器时将其删除

我希望这会对你有所帮助:)。

答案 1 :(得分:1)

  1. 在按钮操作方法中添加此行

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:nil];

  2. 将此代码添加到已实现表格的位置,因此在abVC

    1. viewDidLoad内:
    2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewData) name:@"ReloadTable" object:nil];

      1. 添加以下方法
      2. - (void)reloadTableViewData{
           [self.myTableView reloadData];
        }

        1. 实施dealloc方法(以防止崩溃)
        2. - (void)dealloc{
             [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ReloadTable" object:nil];
          }

相关问题