【RxSwift系列】RXSwift在UITableView中使用(一)

1、RootViewController

let disposeBag = DisposeBag()
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,RootModel>>()
let viewModel = RootViewModel()

//綁定數(shù)據(jù)
viewModel.getNewsData().bind(to: tableView.rx.items(dataSource: dataSource))
        .disposed(by: disposeBag)

//設(shè)置cell
dataSource.configureCell = {
        _, tableView, indexPath, model in
        let cell = RootTableViewCell(style: .default, reuseIdentifier: "cell")
        //處理返回數(shù)據(jù)
        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.itdecent.cn/p/fe36da1267cd
【RxSwift系列】RxSwift下基于MJRefresh實現(xiàn)下拉刷新,上拉加載(三)http://www.itdecent.cn/p/6f14d6a7cb96

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、Moya的使用 1、創(chuàng)建枚舉API 2、實現(xiàn)TargetType協(xié)議 3、發(fā)送網(wǎng)絡(luò)請求 2、添加RXSwift...
    yuanweiphone閱讀 3,596評論 4 21
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評論 25 709
  • 20歲 我想剪一次酷酷的短發(fā) 再染一頭比較另類的發(fā)色 我想嘗試著化妝 只為自己開心 我想去爬一座高山 喊下我的孤獨...
    _咸魚是我閱讀 249評論 0 0
  • 今天晚上和母親大吵了一架 不知道從什么時候開始覺得母親越來越不理解自己 越來越固執(zhí) 越來越霸道 就是你非要按照她選...
    肖贊贊閱讀 308評論 0 1
  • 過去的三十三年,如果不是等來了你的出現(xiàn),該是多么黯淡; 未來的一百三十三年,如果不能和你相陪伴,該是多么凄慘。 人...
    海德成堡閱讀 213評論 0 0

友情鏈接更多精彩內(nèi)容