我有一个带有UICollectionView的ViewController,其元素已绑定,并且通过以下方式创建了单元:
self.viewModel.profileItems.bind(to: self.collectionView.rx.items){ (cv, row, item) ...
我还通过以下方式对用户点击做出反应:self.collectionView.rx.modelSelected(ProfileItem.self).subscribe(onNext: { (item) in
if(/*special item*/) {
let xVC = self.storyboard?.instantiateViewController(identifier: "x") as! XViewController
xVC.item = item
self.navigationController?.pushViewController(xVC, animated: true)
} else {
// other generic view controller
}
}).disposed(by: bag)
xViewController中item的属性为ProfileItem?
类型。如何将XViewController中对item的更改绑定到collectionView单元?提前致谢
最佳答案
您的XViewController
需要一个Observable,该Observable会在适当的时间发出新的项目...然后意识到,该Observable会影响profileItems
或至少是您的视图模型发出的内容。
let xResult = self.collectionView.rx.modelSelected(ProfileItem.self)
.filter { /*special item*/ }
.flatMapFirst { [unowned self] (specialItem) -> Observable<Item> in
let xVC = self.storyboard?.instantiateViewController(identifier: "x") as! XViewController
xVC.item = item
self.navigationController?.pushViewController(xVC, animated: true)
return xVC.observableX // this needs to complete at the appropriate time.
}
self.collectionView.rx.modelSelected(ProfileItem.self)
.filter { /*not special item*/ }
.bind(onNext: { [unowned self] item in
// other generic view controller
}
.disposed(by: bag)
现在,您需要将xResult
输入到您的视图模型中。关于ios - 在RxSwift中观察其他ViewController中的对象更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63928714/