Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
protocol ViewModelDelegate {
func dataLoaded();
}
class ViewModel {
var objects: [Object]?
var delegate: ViewModelDelegate?
func getObjects() {
// .. do something fancy in background here
self.objects = objects
delegate?.dataLoaded();
}
}
class ViewController: UITableViewController, ViewModelDelegate {
func viewDidLoad() {
viewModel.delegate = self;
viewModel.getObjects();
}
func dataLoaded() {
self.tableView.reloadData();
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ObjectCell", for: indexPath)
let data = viewModel.objects[IndexPath.row]
cell.setup(data: data)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment