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?

  1. Must be implementedUITableViewDataSource
  2. Must be able to listen, obtainSequenceData sequence and save
  3. Must beSequenceChanges 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,RxTableViewReactiveArrayDataSourceRelationship 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)

understandDelegateProxyFactoryHow 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

    1. Create an inheritance using the factory methodDelegateProxy, in line with theDelegateProxyTypeObject of the protocol
    2. Use the setForwardToDelegate method to set the object that implements the Protocol
  • Implementation of THE ITMES method of UITableView.rx

    1. Create RxTableViewReactiveArrayDataSourceSequenceWrapper object

      • var itemModels: [Element]?Save data source
      • let cellFactory: CellFactoryCreate a UITableViewCell and leave the closure to the Items method
      • tableView(**_** tableView: , observedElements:)To set the data source, reloadData()
    2. Call subscribeProxyDataSource and do three things

      1. Use the installForwardDelegate method

        • Created using the factory methodRxTableViewDataSourceProxyobject
        • usesetForwardToDelegateMethod to set the implementationUITableViewDataSourceThe object ofRxTableViewReactiveArrayDataSourceSequenceWrapperobject
        • Create a Disposable object to release the resource after the subscription is destroyed
      2. Subscription data after receiving the data calls tableView RxTableViewReactiveArrayDataSourceSequenceWrapper object (* * _ * * tableView: , observedElements:) method to set the data source, reloadData()

      3. Create a Disposable object to release the resource after the subscription is destroyed