2019-05-23 20:23:19 Swift

Swift

Copy Copied! Full
import UIKit import RxSwift final class TableViewController: UIViewController { @IBOutlet private weak var tableView: UITableView! fileprivate var viewModel: TableViewModel! fileprivate lazy var dataSource = TableDataSource(with: self.tableView, viewModel: self.viewModel) static func make(user: User) -> TableViewController { let vc = R.storyboard.tableViewController.instantiateInitialViewController()! vc.viewModel = TableViewModel(user: user) vc.viewModel.fetchUsers() return vc } override func viewDidLoad() { super.viewDidLoad() _ = dataSource viewModel.count.asObservable().subscribe(onNext: { count in print(count) }).disposed(by: rx.disposeBag) viewModel.users.asObservable().observeOn(MainScheduler.instance).subscribe(onNext: { [weak self] users in self?.tableView.reloadData() }).disposed(by: rx.disposeBag) } }