Can't use reloadData from another class

前端 未结 7 2009
栀梦
栀梦 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.

    0 讨论(0)
  • 2021-01-15 04:17

    from the documentation:

    For efficiency, the table view redisplays only those rows that are visible.

    so if your tableView is not visible then it won't get updated.

    0 讨论(0)
  • 2021-01-15 04:20

    When you call classA *test = [[classA alloc]init]; , it creates a new object of your class A and it has nil value for every variables you declare in this class. Thats why your reloaddata is not called becoz your array has nil value for this object which you are using to reload the table.

    You have to use delegate to pass the object of from class A to class B. And then try to reload the table from this delegate object.

    0 讨论(0)
  • 2021-01-15 04:21

    I think u should reload it by delegates.

    Here your ClassB.h Here u need to add your delegate

    @property (nonatomic, strong) id myDelegate;
    

    Here your ClassA.m When u are going to ClassB show his delegate t ClassA

    ClassB *goNext = [[ClassB alloc] initWithNibName:@"ClassB" bundle:nil];
    goNext.myDelegate = self; // ALL YOUR BASE ARE BELONG TO US!
    [self.navigationController pushViewController:goNext animated:YES];
    

    Next. When u want to reload your classA tableView use the code like this:

    if (myDelegate && [myDelegate respondsToSelector:@selector(reloadMyTV)]){
            [myDelegate performSelector:@selector(reloadMyTV) withObject:nil];
        }
    

    where "reloadMyTV" is method of ClassA:

    -(void) reloadMyTV {
    [myTableView reloadData];
    }
    

    Hope I remember delegates good enough ) I wrote it by memory )) Hope it will help

    0 讨论(0)
  • 2021-01-15 04:26
        ClassB.h
    
        @protocol ClassBDelegate <NSObject>
    
        -(void)reloadTableView;
    
    
        @end
    
        @property (nonatomic,weak)id<ClassBDelegate>delegate;
    
    
        ClassB.m
    
        -(void)methodForReloadingTableViewInClassA
        {
    
        [self.delegate reloadTableView];
    
        }
    
    
    
    
    Class A.h
    #import ClassB.h
    @interface ClassA : UITableViewController<UITableViewDelegate,UITableViewDatasource,ClassBDelegate>
    {
    }
    
    ClassA.m   
    -(void)createClassB
    {
        ClassB *obj = [[ClassB alloc]init];
        obj.delegate = self;
    }
    
        //delagte method of class B
        -(void)reloadTableView
        {
    
        [self.tableView reloadData];
    
        }
    
    0 讨论(0)
  • 2021-01-15 04:26

    Calling the reloadData method refreshes the data as soon as the method is called. Make sure the data source (array or dictionary or wherever you've saved the values) is changed before you call reloadData.

    And you should try reloading the data on the main thread : [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

    0 讨论(0)
提交回复
热议问题