Created
February 26, 2017 04:29
-
-
Save suprie/99e3e135717706159fe6d1935b43712f 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 { | |
private var objects: [Object]? | |
var delegate: ViewModelDelegate? | |
func getObjects() { | |
// .. do something fancy in background here | |
self.objects = objects | |
delegate?.dataLoaded(); | |
} | |
func numberOfObjects() -> Int { | |
return objects.count(); | |
} | |
func object(at index: Int) -> Object { | |
return objects[index] | |
} | |
} | |
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.object(at: 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