3

I have a strange problem with my tableView. I load data via JSON into my tableView. While the JSON is being requested from the web in another class, I show an activity indicator view in my current view and the tableView is hidden. I ve got a delegate method, which is called as soon as the json is ready.

-(void)didReceivePlayers:(NSArray *)players {
    [activityIndicator stopAnimating];
    tableViewPlayers.hidden = false;
    startButton.hidden = false;
    playersData = [[NSMutableArray alloc] initWithArray:players];
    [tableViewPlayers reloadData];
    NSLog(@"done reloading"); 
}

The method is being called perfectly. The code is pretty straight forward. I hide my activity indicator and show my tableView. Then I call reloadData. It takes only a few milliseconds. BUT after reloadData, my activityIndicator is still shown and it takes several seconds to show my tableview, although the nslog is being called right away.

I also tried calling reload data in mainThread, but this did not change a thing.

Thanks for your help!

MJN
  • 10,748
  • 1
  • 23
  • 32
Christoph Beger
  • 135
  • 2
  • 14
  • 1
    Both the UI updates, like `startButton.hidden = false` and the `reloadData` *must* be done on the main thread. – Martin R Nov 02 '13 at 17:55
  • Perfect thank you! I only tried reloadData in the mainThread. But the UI changes caused the weird behavior. Thank you! – Christoph Beger Nov 02 '13 at 18:41

1 Answers1

2

Be sure that the code is being executed on the main thread. You can use the main operation queue like this:

-(void)didReceivePlayers:(NSArray *)players {

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [activityIndicator stopAnimating];
        tableViewPlayers.hidden = false;
        startButton.hidden = false;
        playersData = [[NSMutableArray alloc] initWithArray:players];
        [tableViewPlayers reloadData];
        NSLog(@"done reloading");
    }];

}
gimenete
  • 2,649
  • 1
  • 20
  • 16