How to select CollectionView cell in RxSwift

怎甘沉沦 提交于 2019-12-22 04:13:07

问题


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).subscribe(onNext:{ menuItem in }).addDisposableTo(disposeBag) 

Can anybody help?


回答1:


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)
    }


来源:https://stackoverflow.com/questions/43323645/how-to-select-collectionview-cell-in-rxswift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!