The beginning of the nonsense:RxSwiftFor its function, yesswiftThe language is highly encapsulated, but it also uses some OC features, such as exchange method implementations.RxSwiftforUITableViewThe click event is reencapsulated and exchanged insiderespondsToSelectorMethod and rewrite the message forwarding mechanism under theforwardInvocation.RxSwiftThe source code is too complex, therefore, simple to useOCWrite ademoTo understand thisRxSwiftforUITableViewClick event binding.

1. Implementation principle

1, modify an object respondsToSelector method, when in judging method of sel is required to continue, return YES, here is obviously judgment tableView: didSelectRowAtIndexPath: this method. It is important to note that an object that does not comply with the proxy protocol can be executed as long as you implement the proxy method. Compliance with the proxy protocol is just a convenience for developers to implement the proxy method via compiler prompts.

That is to say, let the object as a UITableView executable tableView: didSelectRowAtIndexPath: method of agent, but not to realize the proxy method.

2, modify an object forwardInvocation method, when a method is called an object does not implement the will for message forwarding, so, when forwarding to intercept tableView: didSelectRowAtIndexPath: Method to perform subsequent operations on other objects.

2. Code effect

When a cell is clicked, the response is made through the block shown in the figure above. This is actually a bit like RX in style, but there is no memory management for the object being created. As mentioned below, RxSwift generally has a Disposable return, which is a class that controls when the object created in the sequence is released. You can store disposebags with properties, Dispose destruction can also be done directly at the end of method execution to align the sequence with the current class life cycle.

3, UITableView rxRegistSelected method implementation

Create a category for UITableView:

UITableView+KDS.h

UITableView+KDS.m

Here to circle 1 KDSDelegateProxy class is tableView: didSelectRowAtIndexPath: The proxy method handles the class and provides a delegate for the UITableView with a circle of 2, followed by a call to saveNeedDelegateSel that holds the proxy method required for message forwarding, as explained later.

At this point, UITableView can execute the rxRegistSelected method and pass a UITableViewCell block2 for the block1 returned by this method. Block2 is a concrete implementation of the click event that is actually executed, and block1 is written only to parody RAC.

4. Implementation content of the KDSDelegateProxy object

KDSDelegateProxy.h

KDSDelegateProxy.m

4, KDSTableViewDelegateProxy

KDSTableViewDelegateProxy is follow UITableViewDelegate protocol object and the object to save the cell click coming from the outside agent

5. Summarize and think

RxSwift is much more complicated than the above mentioned. In other words, it is difficult to deduce the details of the solid foundation from a 100-meter tall building with limited personal ability, because after all, it is only a personal idea since it is not a person involved in the construction. So, why isn’t there a Disposable? Because the demo code is static. If you don’t use a global variable, the out-of-scope object will be destroyed and the code won’t work. Dispose dispose on the outer layer as the return value in the inner block to temporarily control the object life cycle in the intermediate process. Or encapsulate something like WSLDisposeBag and store it as a property under, for example, a controller with the same life cycle as the current controller.

Well, the intention of the article is only to share, the code is poor, god do not laugh.