How to select CollectionView cell in RxSwift

前端 未结 2 1218
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 20:55

I need to select the item at specific index in collection view using RxSwift.This method is not working fine.

 collectionView.rx.modelSelected(SearchResult.self         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 21:29

    Building off of mojtaba al moussawi's answer, I made an extension to make the zipping easy:

    extension Reactive where Base: UICollectionView {
        public func modelAndIndexSelected(_ modelType: T.Type) -> ControlEvent<(T, IndexPath)> {
            ControlEvent(events: Observable.zip(
                self.modelSelected(modelType),
                self.itemSelected
            ))
        }
    }
    

    Which you would use like:

    collectionView
        .rx
        .modelAndIndexSelected(SearchResult.self)
        .subscribe(onNext: { (model, index) in
            //Your code
        }).disposed(by: disposeBag)
    

提交回复
热议问题