UITableView reloadData() does not refresh displaye

2019-04-19 18:44发布

问题:

I have

class StationViewController : 
    UITableViewController, UITableViewDelegate, UITableViewDataSource {
  @IBOutlet var stationTableView: UITableView!

When update the data in my data source and do

stationTableView.reloadData()

this is not immediately visible on the screen. If I scroll, tilt or do anything else which forces a repaint of the screen the updated cells are visible. I can confirm that the UITableView did do the update by calling

println("\(stationTableView.visibleCells())")

which prints the expected cells.

The rest of the setup is a UINavigationController which has my StationViewControlleras a relationship. The IBOutlet for the stationViewController is connected to the UITableView in the storyboard file.

I seem to need a "repaint" of the screen to make my update immediately visible. How do I do that?

回答1:

Thanks to Artem, I figured out I need to call reloadData() on the main thread. This is how I solved that:

func refreshUI() {
    dispatch_async(dispatch_get_main_queue(),{
        self.stationTableView.reloadData()
    });
}