Can't use reloadData from another class

陌路散爱 提交于 2019-12-30 11:28:06

问题


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


回答1:


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.




回答2:


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.




回答3:


    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];

    }



回答4:


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.




回答5:


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];




回答6:


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




回答7:


check is your UITableView property is valid at the moment you call reloadData. UIViewController is created by calling alloc init, but your view is currently in invalid state. Set breakpoint in reloadData line of code and check is you .tableView == nil?



来源:https://stackoverflow.com/questions/14979308/cant-use-reloaddata-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!