Objective-c 从另一个 viewcontroller 重新加载 tableview

Objective-c reload tableview from another viewcontroller

我超级困惑。

我有 2 个控制器,我们称它们为 controller1 和 controller2。

在 controller1.m 我有这个:

- (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;
}

在 controller2.m 中,我试图在 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];

}

我试过以下方法:

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

然后是[purchaseOrder.tableView reloadData];

我的 tableView 没有重新加载。什么鬼?我不知道我在这里做错了什么。我也收到此警告:

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

这是警告:

Local declaration of 'tableView' hides instance variable

您需要在 controller2 中引用 controller1。在Controller2.h中声明一个controller1属性.

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

我将假设 controller1 继续到 controller2。所以你可以将controller1的引用传递给prepareForSegue中的controller2。请务必在 Controller1.m 中 #import Controller2.h。在 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
    }  
}

现在 Controller2.m,您可以在您喜欢的地方重新加载 table 视图。

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