I have a simple app that downloads search results in XML when the user types in a UISearchBar
. The download+parsing is threaded and once done it fires an NSNotification
to tell the ViewController with the table view to [tableView reloadData];
我有一个简单的应用程序,当用户在UISearchBar中输入时,它会在XML中下载搜索结果。下载+解析是线程化的,一旦完成,它就会触发一个NSNotification,告诉视图控制器以表视图到[tableView reloadData];
Here is the code that receives the notification fired once results are in:
以下是收到结果时收到通知的代码:
- (void)receivedResults:(id)notification {
results = [notification object];
DLog(@"Received %i results",[results count]);
[[self tableView] reloadData];
}
I get the log output "Received 4 results", but the table view doesn't reload the data until I scroll/drag it a couple pixels. I am using the built-in UITableViewCellStyleSubtitle
cell style and im not changing the height or ding anything fancy with the table view.
我得到了“收到4个结果”的日志输出,但是表视图不会重新加载数据,直到我滚动/拖动它几个像素。我正在使用内置的UITableViewCellStyleSubtitle单元格样式,我不会更改高度或设置任何与表格视图有关的东西。
What am I doing wrong?
我做错了什么?
5 个解决方案
#1
65
I was able to get the same thing to work. But the issue was that the reload data needed to be called on main thread.
我也能做同样的事情。但问题是需要在主线程上调用重载数据。
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
I think this is more practical than the performSelectorOnMainThread option
我认为这比performSelectorOnMainThread选项更实用。
#2
34
Call
调用
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
instead of
而不是
[self.tableview reloadData]
#3
4
My problem was that I was posting a NSNotification from a background thread. To avoid this, simply wrap your postNotificationMethod in a dispatch_async method like this:
我的问题是,我从后台线程发布了一个NSNotification。为了避免这种情况,只需在dispatch_async方法中封装您的postNotificationMethod:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedDownloading" object:result];
});
When this notification will be received, the tableView.reloadData
will be called on the main thread.
当收到此通知时,tableView。将在主线程上调用reloadData。
#4
0
I have the same problem, and I have tried all the solution I can find on google. But All of them don't work.
我有同样的问题,我已经尝试了所有在谷歌上找到的解决方案。但它们都不起作用。
At last I found that I add observer before viewDidLoad, and then [self.tableView reloadData] is not working.
最后,我发现我在viewDidLoad之前添加了observer,然后(self)。tableView reloadData不工作。
First I call the setObserverForUserInfoDatabase in the viewDidLoad of my root navigation view controller. Because I think it was called earlier. The function is like this:
首先,在我的根导航视图控制器的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);
}];}
Then I move the code into viewDidLoad of the viewController itself.
然后我将代码移到viewController本身的viewDidLoad中。
- (void)viewDidLoad {
NSLog(@"View Did Load");
[super viewDidLoad];
[self setObserverForUserInfoDatabase:UserInfoDataBaseAvailabilityNotification];
}
And then everything works fine.
然后一切正常。
I don't know the reason yet. If anyone knows please tell me.
我还不知道原因。如果有人知道请告诉我。
#5
0
reload your tableView
in viewWillLayoutSubviews
在viewWillLayoutSubviews中重新加载tableView。
#1
65
I was able to get the same thing to work. But the issue was that the reload data needed to be called on main thread.
我也能做同样的事情。但问题是需要在主线程上调用重载数据。
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
I think this is more practical than the performSelectorOnMainThread option
我认为这比performSelectorOnMainThread选项更实用。
#2
34
Call
调用
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
instead of
而不是
[self.tableview reloadData]
#3
4
My problem was that I was posting a NSNotification from a background thread. To avoid this, simply wrap your postNotificationMethod in a dispatch_async method like this:
我的问题是,我从后台线程发布了一个NSNotification。为了避免这种情况,只需在dispatch_async方法中封装您的postNotificationMethod:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedDownloading" object:result];
});
When this notification will be received, the tableView.reloadData
will be called on the main thread.
当收到此通知时,tableView。将在主线程上调用reloadData。
#4
0
I have the same problem, and I have tried all the solution I can find on google. But All of them don't work.
我有同样的问题,我已经尝试了所有在谷歌上找到的解决方案。但它们都不起作用。
At last I found that I add observer before viewDidLoad, and then [self.tableView reloadData] is not working.
最后,我发现我在viewDidLoad之前添加了observer,然后(self)。tableView reloadData不工作。
First I call the setObserverForUserInfoDatabase in the viewDidLoad of my root navigation view controller. Because I think it was called earlier. The function is like this:
首先,在我的根导航视图控制器的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);
}];}
Then I move the code into viewDidLoad of the viewController itself.
然后我将代码移到viewController本身的viewDidLoad中。
- (void)viewDidLoad {
NSLog(@"View Did Load");
[super viewDidLoad];
[self setObserverForUserInfoDatabase:UserInfoDataBaseAvailabilityNotification];
}
And then everything works fine.
然后一切正常。
I don't know the reason yet. If anyone knows please tell me.
我还不知道原因。如果有人知道请告诉我。
#5
0
reload your tableView
in viewWillLayoutSubviews
在viewWillLayoutSubviews中重新加载tableView。