简体   繁体   中英

Objective-c reload tableview from another viewcontroller

I am super confused.

I have 2 controllers, lets call them controller1 and controller2.

In controller1.m I have this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

and in controller2.m I am trying to reload the tableview in controller1.m:

- (void)GetRequest
{

    NSArray *tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];

    if(!purchaseOrder.objects){
        purchaseOrder.objects = [[NSMutableArray alloc]init];
    }

    for(int i = 0; i < [tableData count]; i++){
        [purchaseOrder.objects addObjectsFromArray:[tableData objectAtIndex:i]];
        NSLog(@"%@",[tableData objectAtIndex:i]);
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [purchaseOrder.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    NSLog(@"%@", purchaseOrder.objects);

    //[self.tableView reloadData];

}

I have tried the following:

controller1.h:

@property(nonatomic, retain) UITableView *tableView;

controller1.m:

@synthesize tableView;

controller2.h:

#import "controller1.h"

@interface controller2 ()
{


    controller1 *purchaseOrder;

}

- (void)viewDidLoad {
     purchaseOrder = [[controller1 alloc]init];
}

and then [purchaseOrder.tableView reloadData];

and my tableView doesnt get reloaded. WTF ? I have no idea what I am doing wrong here. I also get this warning on:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

and here is the warning:

Local declaration of 'tableView' hides instance variable

You need a reference to controller1 within controller2 . In Controller2.h, declare a controller1 property.

#import "Controller1.h"
@interface Controller2 : UIViewController
@property (nonatomic, strong) Controller1 *controller1;
@end

I am going to make the assumption that controller1 is segueing to controller2 . So you can pass the reference of controller1 to controller2 in prepareForSegue . Be sure to #import Controller2.h in Controller1.m. In Controller1.m:

- (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[Controller2 class]]) {
        Controller2 *controller2 = (Controller2 *)segue.destinationViewController;
        controller2.controller1 = self;  // now you can reference the tableView in controller2
    }  
}

Now in Controller2.m, you can reload the table view where you like.

- (void)GetRequest
{
    // ...
    [self.controller1.tableView reloadData];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM