2

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

Segev
  • 19,035
  • 12
  • 80
  • 152
  • 1
    One option is to use the delegate method. Here is an [example][1] for how to. [1]: http://stackoverflow.com/questions/8055052/call-a-parent-view-controller-through-a-navigationcontroller – user523234 Feb 20 '13 at 12:06

7 Answers7

15

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.

aBilal17
  • 2,974
  • 2
  • 17
  • 23
1

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.

riyaz
  • 514
  • 4
  • 17
1
    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];

    }
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
  • I'm getting `Cannot find protocol declaration for 'ClassBDelegate' ` – Segev Feb 20 '13 at 12:29
  • have you imported ClassB.h in ClassA.h? – Vishal Singh Feb 20 '13 at 12:47
  • in which class you are getting this error? if in ClassB then declare protocol at the top of ClassB.h... above everything... – Vishal Singh Feb 20 '13 at 12:54
  • I'm getting it in classA here: `@interface classA : UIViewController {` – Segev Feb 20 '13 at 12:56
  • i am not sure waht is causing the problem if you have realy imported Classb in classA.. have to look in the code – Vishal Singh Feb 20 '13 at 13:00
  • I've just created a new fresh project with your code. I'm still getting the same error. You can find the orignal code with the error here: http://pastebin.com/GEB42nWy – Segev Feb 20 '13 at 13:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24831/discussion-between-ivishal-and-sha) – Vishal Singh Feb 20 '13 at 13:30
  • 1
    @Sha There might be a case when looping is being formed. If `Class A` is being imported in `Class B`, and so is `Class B` in `Class A`. If that is the case, declare the protocol in separate file. – Zen Feb 28 '13 at 07:28
0

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

Arthur
  • 1,740
  • 3
  • 16
  • 36
0

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.

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
0

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

Suhaiyl
  • 1,071
  • 9
  • 17
0

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?

iiFreeman
  • 5,165
  • 2
  • 29
  • 42