Created
February 26, 2017 04:28
-
-
Save suprie/ebdc7cada6373fafabd2749947c9a9e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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