【问题标题】:Objective-c reload tableview from another viewcontrollerObjective-c 从另一个视图控制器重新加载表视图
【发布时间】:2015-04-30 19:18:00
【问题描述】:

我很困惑。

我有 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 中重新加载 tableview:

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

【问题讨论】:

  • 似乎还有另一个变量名'tableView'。您需要重命名它或确保您的目标是正确的。请显示更多代码以进行检查。
  • 这有很多奇怪的地方,但让我们从 purchaseOrder = [[controller1 alloc]init]; 开始。这将创建一个 controller1。如果你想让purchaseOrder更新,你需要先推送或呈现。我怀疑你可能已经在其他地方有一个controller1,但这个新的与屏幕上可能已经出现的任何东西都没有关系。

标签: ios objective-c xcode uitableview uiviewcontroller


【解决方案1】:

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

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

我将假设controller1controller2 连接。所以你可以将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中,你可以在你喜欢的地方重新加载表格视图。

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

【讨论】:

  • 我不明白我应该在 prepareForSegue 中放入什么(在...之后)
  • controller1继承自UIViewController的方法。我不记得实际的语法,所以我抄了下来,然后把...开始在控制器 1 中输入“-prepareFor”,然后自动完成应该为您填写其余部分。
  • 编辑我的答案以填写方法名称。
  • 您的代码出现 3 个错误:1. 在 prepareForSegue 方法中需要一个类型,2. 在对象类型 '__strong id' 上找不到destinationController 和 3 在对象类型控制器2 上找不到属性控制器1跨度>
  • 1.将destinationViewController 转换为Controller2。 2. 是destinationViewController 而不是destinationController。此外,看起来您在应该是 segue.destinationViewController 时执行了 sender.DestinationViewController。 3. 确保将标头#import 到 Controller2 以查看我们添加的 controller1 属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
相关资源
最近更新 更多