gpt4 book ai didi

ios - 从 UICollectionView 索引路径获取模型

转载 作者:行者123 更新时间:2023-12-01 15:53:59 25 4
gpt4 key购买 nike

我正在使用 RxSwift将模型数组绑定(bind)到 Collection View

如何从给定的 indexPath 获取模型对象?

我正在做这样的绑定(bind):

vm.bikeIssueCatagories()
.drive(self.collectionView.rx.items(cellIdentifier: "BikeIssueCategoryCollectionViewCell", cellType: UICollectionViewCell.self)) { row, data, cell in
}.disposed(by: disposeBag)

我的问题的核心是,我需要同时获取模型对象和用户选择的单元格。使用 collectionView.rx.modelSelected(T.self)只给我模型 og 类型 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)

但这在尝试使用 indexPath 的模型时给了我一个错误:

Type 'inout UICollectionView' does not conform to protocol 'ReactiveCompatible'



尝试一下:
let indexPath = IndexPath.init()
self.collectionView.rx.model(at: indexPath)

也给了我一个错误:

Ambiguous reference to member 'model(at:)'



SO...如何同时获取模型对象和用户选择的单元格?

最佳答案

我本可以像 RamwiseMatt 提议的那样做。在我的 ViewModel 上创建一个采用 IndexPath 的方法并返回模型。然而,我最终还是使用了 zip :

let modelSelected = collectionView.rx.modelSelected(SelectableBikeIssueCategory.self).asObservable()

let cellSelected = collectionView.rx.itemSelected.asObservable()
.map { [weak self] indexPath -> UICollectionViewCell in
guard let cell = self?.collectionView.cellForItem(at: indexPath) else { fatalError("Expected cell at indexpath") }
return cell
}

Observable.zip(cellSelected, modelSelected)
.map { [weak self] cell, category -> SomeThing in
return SomeThing()
}

关于ios - 从 UICollectionView 索引路径获取模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46811630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com