【问题标题】:objective-c reload UITableView after back navigation后退导航后objective-c重新加载UITableView
【发布时间】:2013-07-10 07:48:28
【问题描述】:

我在NavigationController 中嵌入了一个TableViewController,在某些时候我使用Seguepush 到一个新的ViewController。当我从ViewController 导航回来时,我想在TableViewController 中重新加载tableView

我遵循herehere 发布的解决方案。由于某种原因,它不起作用。我想念一些东西,但我现在看不到它。

谁有胶水,为什么代码不起作用?我该怎么做才能让它工作?

TableViewController.m

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    // notification when comeing back from the cameraView
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMagazine:) name:@"reloadTable" object:nil];

    }
    return self;
} 
//[...]
// notification reload
-(void)reloadMagazine:(NSNotification *)notification {

    NSLog(@"notification reload");
    [self.tableView reloadData];
}
//[...]

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTable" object:self];
}

ViewController.m

-(void)uploadData:(id)sender {    
    // Upload and navigate back to the tableViewController
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTable" object:self];

    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];

}

【问题讨论】:

  • 你为什么不直接打电话给reloadData in viewWillAppear
  • 当然你不应该同时调用dismissViewControllerAnimated 和popViewControllerAnimated。坚持前者。
  • ... 你应该在某处移除观察者,可能在dealloc
  • 我认为导航返回时不会调用viewWillAppear。会试试这个。
  • viewWillAppear 合作。谢谢。删除 dismissViewControllerAnimatedpopViewControllerAnimated 单独工作正常。反之则不行。

标签: objective-c uitableview navigation reload nsnotification


【解决方案1】:

这并不能回答您的问题“为什么代码不起作用?”,但最简单的 导航回表视图时重新加载表视图的方法可能是 在viewWillAppear 中调用reloadData

【讨论】:

    【解决方案2】:

    我怀疑上面的代码不起作用,因为没有调用 initWithStyle。在那里设置一个断点来测试我的理论。如果是这种情况,则将该代码移至 viewDidLoad。

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2011-04-08
      相关资源
      最近更新 更多