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

后端 未结 5 1849
南方客
南方客 2021-02-01 18:57

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 NSNo

相关标签:
5条回答
  • 2021-02-01 19:25

    reload your tableView in viewWillLayoutSubviews

    0 讨论(0)
  • 2021-02-01 19:26

    Call

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

    instead of

    [self.tableview reloadData]
    
    0 讨论(0)
  • 2021-02-01 19:28

    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

    0 讨论(0)
  • 2021-02-01 19:50

    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.

    0 讨论(0)
  • 2021-02-01 19:50

    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.

    0 讨论(0)
提交回复
热议问题