我正在尝试观察自定义类委托。我从
开始public var didTapAvatar: Observable<()> {
return delegate
.methodInvoked(#selector(JSQMessagesCollectionViewDelegateFlowLayout.collectionView(_:didTapAvatarImageView:at:)))
.map { _ in ()
}
}
会导致如下错误
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [RxCocoa.RxCollectionViewDelegateProxy collectionView:layout:heightForCellTopLabelAtIndexPath:]:无法识别的选择器发送到实例0x618000298b00
我后来尝试了这个
public var jsqdelegate: DelegateProxy {
return RxJSQMessageCollectionViewCellProxy(parentObject: base)
}
public var didTapAvatar: Observable<()> {
return jsqdelegate
.methodInvoked(#selector(JSQMessagesCollectionViewDelegateFlowLayout.collectionView(_:didTapAvatarImageView:at:)))
.map { _ in ()
}
将成功运行,但会立即完成并按照打印出来放置到控制台上进行处理:
self.collectionView.rx.didTapAvatar.asObservable()
.subscribe(onNext: { (event) in
print("next")
}, onError: { (error) in
print("error")
}, onCompleted: {
print("complete")
}, onDisposed: {
print("disposed")
}).disposed(by: disposeBag)
RxJSQMessageCollectionViewCellProxy.swift
public class RxJSQMessageCollectionViewCellProxy: DelegateProxy, JSQMessagesCollectionViewDelegateFlowLayout, DelegateProxyType {
public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
let collectionView: JSQMessagesCollectionView = object as! JSQMessagesCollectionView
return collectionView.delegate
}
public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
let collectionView: JSQMessagesCollectionView = object as! JSQMessagesCollectionView
collectionView.delegate = delegate as? JSQMessagesCollectionViewDelegateFlowLayout
}
}
JSQMessagesCollectionView + RxCreate.swift
extension Reactive where Base: JSQMessagesCollectionView {
public var didTapAvatar: Observable<()> {
return delegate.methodInvoked(#selector(JSQMessagesCollectionView.messagesCollectionViewCellDidTapAvatar(_:))).map { _ in () }
}
}