moonvader
moonvader

Reputation: 21101

Who is calling reloadData method for UITableView

It can sounds weird but I don't understand why my tableView is showing cells.

I got array of items that should be shown in cells but I don't run reloadData method of my tableView anywhere in my code. It seems that some of app components or maybe frameworks inside app is calling reloadData method and I want to find out which one?

How it can be done?

Upvotes: 0

Views: 75

Answers (2)

André Slotta
André Slotta

Reputation: 14040

If your data preparation takes some time and you do not want the table view to show any data initially you could use an approach like this:

class TableViewController: UITableViewController {

    var someDataSource: [Any]!
    var dataSourcePrepared = false {
        didSet {
            tableView.reloadData()
        }
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        guard dataSourcePrepared else { return 0 }
        return someDataSource.count
    }

    func doSomePreparationStuff() {
        // ...
        // ...
        someDataSource = ["Some", "Content"]
        dataSourcePrepared = true
    }

}

In this case I used a Bool variable dataSourcePrepared which is false initially. As soon as you have prepared your content set it to true and the table view gets reloaded.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318854

A table view loads itself the first time it is added to the window hierarchy. You don't need an explicit call to reloadData for the table to load itself initially.

If you want to see how this is really done, put a breakpoint on your table view data source methods and bring up your table view. Look at the stack trace in the debugger to see the sequence of events.

Upvotes: 2

Related Questions