Can't use reloadData from another class

前端 未结 7 2068
栀梦
栀梦 2021-01-15 03:56

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 <

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 04:13

    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.

提交回复
热议问题