Skip to content

Instantly share code, notes, and snippets.

@mecid
Created January 8, 2018 19:47
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 mecid/a9c0026dd5ebdc68e383e075e37ac1db to your computer and use it in GitHub Desktop.
Save mecid/a9c0026dd5ebdc68e383e075e37ac1db to your computer and use it in GitHub Desktop.
Mastering MVVM
class ItemsViewController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
private let activityIndicator = ActivityIndicatorView()
private var viewModel: ItemsViewModel
init(viewModel: ItemsViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
bindViewModel()
viewModel.fetch()
}
func bindViewModel() {
viewModel.refreshing.bind(to: activityIndicator.reactive.isAnimating)
viewModel.items.bind(to: self) { strongSelf, _ in
strongSelf.tableView.reloadData()
}
}
func setupUI() {
view.addSubview(activityIndicator)
}
}
extension ItemsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.items.value.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment