[Car Brand] has [Types] has Year or specific model (string)
例如
BMW > Z4 > 2005
BMW > Z3 > 1999
BMW > Z3 > 2001
Porshe > Carrera > 1999
Audi > TT > 2002
选择后必须更改yearView背景颜色。我向yearView添加了一个tapGesture,然后尝试监听Type Cell中的所有yearView
var carForType = PublishSubject<SelectedCar>() // in class property
let bv = yearContainer(frame: .zero, withOption: option)
bv.tap.rx.event.asObservable().map({ _ -> Selected in
return SelectedBet.init(car: nil, type: self.type, year: option)
})
.bind(to: self.typeForCar)
.disposed(by: self.disposeBag)
我正在尝试从 TableVC 获取所有选定的汽车
cell.selectedCars.debug().subscribe(onNext: {
var car = $0
pBet.brand = self.viewModel.brand
print(car)
}).disposed(by: self.disposeBag)
但是滚动后,它会双重订阅单元格,总的来说,我开始认为应该有更好的方法来做到这一点。如果能就如何简化提出意见那就太好了,我愿意改变所有的结构。
注意:项目正在使用 MVVM 和 RxDataSources 扩展。
最佳答案
感谢 RxSwift 社区,我已经成功找到了相同的用例。正如所解释的here从kzaher来看,cell使用的disposeBags需要在cell的prepareForReuse方法中释放。
但我使用了另一种方法,不需要在单元格中使用 disposeBag。我将所有的tapEvents添加到单元格中的一个数组中,并将其合并到一个可观察量中,然后在tableView中通过将所有单元格可观察量合并到一个可观察量中来执行相同的操作,当然在合并之前,我将tapObservables映射到模型中,该模型保存有关所有汽车的信息细节。它变得像责任链模式 [ 2 ]
这是了解更多详细信息的代码。
所有订阅均在 VC 中进行。
dataSource = RxTableViewSectionedReloadDataSource<MarketsSectionData>(configureCell: { (dataSource, tableView, indexPath, _) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath) as? AllCarsCell else {
return UITableViewCell()
}
let market = dataSource.sectionModels[indexPath.section].items[indexPath.row]
cell.setUpWith(market: market)
let sel = cell.selectionsForMarket?.map({ car -> SelectedCar in
var tCar = car
tCar.brand = self.viewModel.brand
return tCar
})
self.selectionCells.onNext(sel!)
cell.findSelectedCellAndFill(withId: self.viewModel.currentCar?.id)
self.viewModel.selection.subscribe(onNext: { car in
cell.findSelectedCellAndFill(withId: car.option?.id)
}, onDisposed: {
print("deque is disposed")
}).disposed(by: self.disposeBag)
return cell
})
self.selectionCells.merge().bind(to: self.viewModel.selection).disposed(by: self.disposeBag)
在单元格中,我将所有 View 连接到一个可观察的内容中,就像一样
self.selectionsForMarket = Observable.from(tapActions).merge()
并且 View 是无状态的,只有 TapGesture。
关于ios - 如何使用 RxSwift 管理绑定(bind)到 TableViewCell 的树状数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53729290/