RxSwift double mapping in tableView.rx.itemSelected
If you need both indexPath
and the model
you could combine the 2 methods and transform them in only one observable sequence:
Observable
.zip(tableView.rx.itemSelected, tableView.rx.modelSelected(String.self))
.bind { [unowned self] indexPath, model in
self.tableView.deselectRow(at: indexPath, animated: true)
print("Selected " + model + " at \(indexPath)")
}
.disposed(by: disposeBag)
RxCocoa has 2 methods:
public var itemSelected: ControlEvent<IndexPath> { get }
which returns IndexPath
of selected item
and public func modelSelected<T>(_ modelType: T.Type) -> ControlEvent<T>
which returns the model element
In accordance to your example it will look:
tableView.rx.modelSelected(Item.self)
.subscribe(onNext: { [weak self] item in
// other actions with Item object
}).addDisposableTo(disposeBag)