Who is calling reloadData method for UITableView
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?
reloadData
reloadData
How it can be done?
reloadData
the problem is that tableView is showing content before it is properly prepared
– moonvader
Jun 30 at 7:15
you are barking under wrong tree. Issue is most probably how you manage data to show and how you are handling notifications from
UITableViewDataSource
and UITableViewDelegate
.– Marek R
Jun 30 at 8:35
UITableViewDataSource
UITableViewDelegate
take a look here. It is OS X example, but pattern is the same (differences are minimal).
– Marek R
Jun 30 at 8:40
Just saw your comment about showing content before it is properly prepared. You should post a question specific to that issue. Include relevant code.
– rmaddy
Jun 30 at 14:42
2 Answers
2
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.
reloadData
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.
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.
Bool
dataSourcePrepared
false
true
There's a more direct solution. Get rid of
dataSourcePrepared
and make someDataSource
a proper optional (with ?
).– rmaddy
Jun 30 at 14:41
dataSourcePrepared
someDataSource
?
@rmaddy Sure. If the non nil data source is the only criterium.
– André Slotta
Jun 30 at 14:42
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
why this is a problem?
reloadData
shouldn't be overridden (there is no reason).– Marek R
Jun 30 at 6:35