RxSwift源码分析(22)——TableView
2020-11-14 本文已影响0人
无悔zero
上一篇介绍了中介者,今天的探索也跟中介者有关,中介者真的非常多。直接来看看下面的例子:
let dataOb = BehaviorSubject.init(value: dataArray)
//绑定数据源
dataOb.asObserver()
.bind(to: tableView.rx.items(cellIdentifier: "TestTableViewCell", cellType: TestTableViewCell.self)){ (row, model, cell) in
cell.setUIData(model as! TestModel)
}.disposed(by: disposeBag)
//点击事件
tableView.rx.itemSelected
.subscribe(onNext: { (indexPath) in
print("点击")
}).disposed(by: disposeBag)
有了RxSwift使用tableview
真的很方便,再也不用自己实现各种代理和协议,那我们就来看看它是怎么帮我们做这些的(就看看流程好了😨):
- 进入源码,马上看到
cell
的创建:
![](https://img.haomeiwen.com/i2141351/bb66901e662de476.png)
![](https://img.haomeiwen.com/i2141351/47f916abd945f504.png)
原来是_RxTableViewReactiveArrayDataSource
实现了datasource
的方法。
- 接着下一步先是看到了
delegate
的设置:
![](https://img.haomeiwen.com/i2141351/0a76dfa3a29145af.png)
![](https://img.haomeiwen.com/i2141351/780551a1037e2f84.png)
![](https://img.haomeiwen.com/i2141351/c7705bdbaee737fe.png)
![](https://img.haomeiwen.com/i2141351/dc61eba1917a772f.png)
![](https://img.haomeiwen.com/i2141351/fb80cec036fc1826.png)
- 然后返回销毁者之前又设置了
datasource
:
![](https://img.haomeiwen.com/i2141351/ce134facc4f90f36.png)
![](https://img.haomeiwen.com/i2141351/a0ad6786b1517008.png)
原来再一次走这个函数方法:
![](https://img.haomeiwen.com/i2141351/11dd0fbb14f3cb69.png)
![](https://img.haomeiwen.com/i2141351/03859d2bfe773a88.png)
![](https://img.haomeiwen.com/i2141351/366b7d47f762ac0a.png)
-
tableView
的显示完成了,最后就是交互事件的实现:
![](https://img.haomeiwen.com/i2141351/3300c949b1d632f0.png)
通过self.delegate.methodInvoked
便可在外面的响应闭包回调交互事件。