[tableView reloadData]; doesn't work until I scroll the tableView

巧了我就是萌 提交于 2020-01-11 17:39:11

问题


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

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.

What am I doing wrong?


回答1:


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




回答2:


Call

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

instead of

[self.tableview reloadData]



回答3:


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:

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.




回答4:


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.

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:

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

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


reload your tableView in viewWillLayoutSubviews



来源:https://stackoverflow.com/questions/4968424/tableview-reloaddata-doesnt-work-until-i-scroll-the-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!