How to select CollectionView cell in RxSwift

前端 未结 2 1217
隐瞒了意图╮
隐瞒了意图╮ 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:45

    If you want the indexPath of item selected you can use the following :

    collectionView
        .rx
        .itemSelected
            .subscribe(onNext:{ indexPath in
                //your code
            }).disposed(by: disposeBag)
    

    and if you want to the model being selected :

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

    And you can combine the above, to get the modelSelected with their indexPath as follow:

      Observable
                .zip(
                    collectionView
                        .rx
                        .itemSelected
                    ,collectionView
                        .rx
                        .modelSelected(SearchResult.self)
                )
                .bind{ [unowned self] indexPath, model in
    
                }
                .disposed(by: disposeBag)
        }
    

提交回复
热议问题