This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge
preface
_ = Observable .of(["1", "2", "3"]) .bind(to: tableView.rx.items) { tb , ip, item in let cell = tb.dequeueReusableCell(withIdentifier: "cell")! cell.textLabel? .text = item return cell } .disposed(by: disposeBag)Copy the code
You can quickly display a tableView by binding the Items method of TableView.rx to the Sequence data Sequence
Think: How is the Items method implemented, and what functions must the Items method perform in order to display a tableView?
- Must be implemented
UITableViewDataSource
- Must be able to listen, obtain
Sequence
Data sequence and save - Must be
Sequence
Changes are automatically refreshedTableView
understandbind(to: )
Method to expand grammar sugar
public func bind<R1, R2>(to binder: (Self) -> (R1) -> R2, curriedArgument: R1) -> R2
Copy the code
_ = Observable .of(["1", "2", "3"]) .bind(to: tableView.rx.items, curriedArgument: { tb , ip, item in let cell = tb.dequeueReusableCell(withIdentifier: "cell")! cell.textLabel? .text = item return cell }) .disposed(by: disposeBag)Copy the code
Understand the items (_Source: source) method
Understand the items(dataSource: dataSource) method
Items (dataSource: dataSource) implementation with simplified logic
// Set TableView UITableViewDataSource as a dataSource object let unregisterDelegate = RxTableViewDataSourceProxy.installForwardDelegate(dataSource, retainDelegate: true, onProxyForObject: Let reloadTableView = source.subscribe {[Weak tableView = self.base] event in guard let tableView = tableView else {return} // dataSource: Update the data source RxTableViewReactiveArrayDataSourceSequenceWrapper < / String > The var itemModels: (Element)? // tableView.reloadData() update tableView dataSource. TableView (tableView, observedEvent: } // Return a destructible object. Upon destruction, At the same time destroy unregisterDelegate and reloadTableView return Disposables. Create {unregisterDelegate. Dispose () reloadTableView.dispose() }Copy the code
understandUITableView
,RxTableViewDataSourceProxy
,RxTableViewReactiveArrayDataSource
Relationship between
Summary of code snippets in this relationship
Get RxTableViewDataSourceProxy object (DelegateProxyType. Swift / 181)
Set TableViewDataSource (DelegateProxyType. Swift / 285)
Set the redirection object _setForwardToDelegate (_rxDelegateProxy.m /95 line)
Message Redirection (_RXDelegateProxy /117 Line)
understandDelegateProxyFactory
How do I create a DelegateProxyType
Classes conforming to the DelegateProxyType protocol create a unique factory, all stored in the _sharedFactories dictionary
A method that generates a Proxy object by storing a class with the _factories dictionary
When you create the factory object, add the closure that creates the Proxy object. The Proxy object is then created by calling the closure
New ideas for factory implementation:
Implement factory methods through dictionaries and closures
Conclusion:
-
Use Rx to set up the underlying implementation of the agent
- Create an inheritance using the factory method
DelegateProxy
, in line with theDelegateProxyType
Object of the protocol - Use the setForwardToDelegate method to set the object that implements the Protocol
- Create an inheritance using the factory method
-
Implementation of THE ITMES method of UITableView.rx
-
Create RxTableViewReactiveArrayDataSourceSequenceWrapper object
var itemModels: [Element]?
Save data sourcelet cellFactory: CellFactory
Create a UITableViewCell and leave the closure to the Items methodtableView(**_** tableView: , observedElements:)
To set the data source, reloadData()
-
Call subscribeProxyDataSource and do three things
-
Use the installForwardDelegate method
- Created using the factory method
RxTableViewDataSourceProxy
object - use
setForwardToDelegate
Method to set the implementationUITableViewDataSource
The object ofRxTableViewReactiveArrayDataSourceSequenceWrapper
object - Create a Disposable object to release the resource after the subscription is destroyed
- Created using the factory method
-
Subscription data after receiving the data calls tableView RxTableViewReactiveArrayDataSourceSequenceWrapper object (* * _ * * tableView: , observedElements:) method to set the data source, reloadData()
-
Create a Disposable object to release the resource after the subscription is destroyed
-
-