2011-02-11 80 views
21

我有一个简单的应用程序,用户输入UISearchBar时,下载XML搜索结果。下载+解析有螺纹,做过一次触发一个NSNotification与表视图告诉视图控制器到[tableView reloadData];[tableView reloadData];不工作,直到我滚动tableView

这里是接收触发一次结果通知的代码是:

- (void)receivedResults:(id)notification { 
    results = [notification object]; 
    DLog(@"Received %i results",[results count]); 
    [[self tableView] reloadData]; 
} 

我得到日志输出“收到4结果”,但表格视图不重新加载数据,直到我滚动/拖动它几个像素。我正在使用内置的UITableViewCellStyleSubtitle单元格样式,并且不会改变高度,也不会改变表格视图中的任何内容。

我在做什么错?的

[self.tableview reloadData] 
+0

结果当然是一个实例变量和表视图源,否则搜索后,周围拉着表视图时,上面的代码甚至不会工作。 – 2011-02-11 11:25:51

+0

你确定[self tableView]不会返回零?可能是重新加载没有响应的原因,但cellForRowAtIndexPath会。该方法由委托调用。 – 2011-02-19 09:04:28

回答

65

+0

不知道它是否更加实用,但Apple会推荐GCD以支持performSelector ...模式。 – 2013-02-28 09:22:57

34

呼叫

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 

,而不是我能得到同样的东西的工作。但问题是重载数据需要在主线程上调用。

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.tableView reloadData]; 
}); 

我认为这是比performSelectorOnMainThread选项

+0

不错,我正在重新加载我的数据在NSNotification的事件处理程序。 – NMunro 2014-10-08 20:34:12

4

我的问题是,我是从发布一个后台线程NSNotification更加实用。为了避免这种情况,只是包装你postNotificationMethod在这样的dispatch_async方法:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedDownloading" object:result]; 
}); 

当这个通知将被接收,该tableView.reloadData将在主线程中调用。

0

我有同样的问题,我尝试了所有可以在google上找到的解决方案。但所有这些都不起作用。

最后我发现我在viewDidLoad之前添加观察者,然后[self.tableView reloadData]不起作用。

首先我在我的根导航视图控制器的viewDidLoad中调用setObserverForUserInfoDatabase。因为我认为它早些时候被调用过。功能是这样的:

- (void)setObserverForUserInfoDatabase:(NSString *)name { 
    [[NSNotificationCenter defaultCenter] addObserverForName:name 
                 object:nil 
                 queue:nil 
                usingBlock:^(NSNotification *note) { 
                 [self loadUserInfo]; 
                 [self.tableView reloadData]; 
                 NSLog(@"User Panel Notification \n %@", self); 
                }];} 

然后我将代码移动到viewController本身的viewDidLoad。

- (void)viewDidLoad { 
    NSLog(@"View Did Load"); 
    [super viewDidLoad]; 

    [self setObserverForUserInfoDatabase:UserInfoDataBaseAvailabilityNotification]; 
} 

然后一切工作正常。

我不知道原因。如果有人知道,请告诉我。

0

重装tableViewviewWillLayoutSubviews