Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView reloadData() does not refresh displayed cells

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?

like image 286
Erik Henriksson Avatar asked Sep 21 '14 10:09

Erik Henriksson


People also ask

How do you reload a particular cell in UITableView Swift?

Reloading a cell/row at an IndexPath is easy and all you need to use is the reloadRows(at:, with:) .

How to reload UITableView?

If you want to reload your table view while also saving and restoring any selections, you should take a copy of the indexPathsForSelectedRows property before the reload, then re-apply those selections after calling reloadData() . With that in place, you can now call yourTableView.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

Is it possible to add UITableView within a Uitableviewcell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.


1 Answers

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()
    });
}
like image 103
Erik Henriksson Avatar answered Sep 21 '22 20:09

Erik Henriksson