我正在使用RxSwift将模型数组绑定到集合视图
如何从给定的 indexPath 获取模型对象?
我正在做这样的绑定:
vm.bikeIssueCatagories()
.drive(self.collectionView.rx.items(cellIdentifier: "BikeIssueCategoryCollectionViewCell", cellType: UICollectionViewCell.self)) { row, data, cell in
}.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
我的问题的核心是,我需要获取用户选择的模型对象和单元格。Using collectionView.rx.modelSelected(T.self)only 给了我模型 og type T。打电话collectionView.rx.itemSelected只给我选择的IndexPath
collectionView.rx.itemSelected.asDriver()
.driveNext { [unowned self] indexPath in
guard let model = try? collectionView.rx.model(at: indexPath) else { return }
guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
}.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在 indexPath 处创建模型时,这给了我一个错误:
类型“inout UICollectionView”不符合协议“ReactiveCompatible”
试试看:
let indexPath = IndexPath.init()
self.collectionView.rx.model(at: indexPath)
Run Code Online (Sandbox Code Playgroud)
也给了我一个错误:
对成员“model(at:)”的不明确引用
SO...如何获取用户选择的模型对象和单元格?