Skip to content

Instantly share code, notes, and snippets.

@suprie
Created February 26, 2017 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suprie/ebdc7cada6373fafabd2749947c9a9e3 to your computer and use it in GitHub Desktop.
Save suprie/ebdc7cada6373fafabd2749947c9a9e3 to your computer and use it in GitHub Desktop.
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