Redraw UITableView after updating data async

Viewed 40021

I have a UITableview that I load with data async so the tableview might appear without data.
I have tired the ReloadData method but the tableview remains empty until I scroll the tableview, suddenly the data appears.
The same thing happens when I load a tableview as a detailedview and switching between items, the previoud items data appears first and as soon as I scroll in the table view it shows the correct data.
My guess is that the ReloadData method works just fine, but I need to redraw the tableview somehow, any suggestions on how to solve this?

/Jimmy

6 Answers

You said you're populating content asynchronously but did you invoke the reloadData in the context of the main thread ? (and not via the thread that populates the content)

Objective-C

[yourUITableView performSelectorOnMainThread:@selector(reloadData)
                                  withObject:nil 
                               waitUntilDone:NO];

Swift

dispatch_async(dispatch_get_main_queue(), { self.tableView.reloadData() })

Monotouch

InvokeOnMainThread(() => this.TableView.ReloadData());

I guess it wasn't reloaded.

When you scroll the cells to out of screen, then…

tableView: cellForRowAtIndexPath:

…will be called. So it will be reloaded.

I guess UITableView variable is not validated.

If you use UITableView as a main view, you can try this.

[self.view reloadData];

or

[self.tableView reloadData];

Swift 4:

DispatchQueue.main.async { self.tableView.reloadData() }
Related