1、RootViewController
let disposeBag = DisposeBag()
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,RootModel>>()
let viewModel = RootViewModel()
//绑定数据
viewModel.getNewsData().bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
//设置cell
dataSource.configureCell = {
_, tableView, indexPath, model in
let cell = RootTableViewCell(style: .default, reuseIdentifier: "cell")
//处理返回数据
cell.model = model
return cell
}
//cell点击事件
tableView.rx.itemSelected.map { indexPath in
return (indexPath,self.dataSource[indexPath])
}
.subscribe(onNext: { indexPath, model in
self.navigationController?.pushViewController(FirstViewController(), animated: true)
})
.disposed(by: disposeBag)
或者用
tableView.rx
.modelSelected(FirstModel.self)
.subscribe(onNext: { (model) in
self.navigationController?.pushViewController(FirstViewController(), animated: true)
})
.addDisposableTo(disposeBag)
2、RootViewModel
func getNewsData() -> Observable<[SectionModel<String, RootModel>]> {
return Observable.create{ (observer) -> Disposable in
let array = [RootModel(name: "111", age: 10), RootModel(name: "222", age: 20), RootModel(name: "333", age: 30)]
let section = [SectionModel(model: "", items: array)]
observer.onNext(section)
observer.onCompleted()
return Disposables.create()
}
}
3、RootModel
struct RootModel {
let name: String
let age: Int
}
4、RootTableViewCell
var model: RootModel? {
didSet {
nameLabel.text = model.name
ageLabel.text = model.age
}
}
【RxSwift系列】Moya + RXSwift + HandyJSON在UITableView中的使用(二)http://www.jianshu.com/p/fe36da1267cd
【RxSwift系列】RxSwift下基于MJRefresh实现下拉刷新,上拉加载(三)http://www.jianshu.com/p/6f14d6a7cb96