我有一个parentView
,其中包含两个不同的CollectionViwes
。通过使用Observable
将数据传递到BehaviorRelay
,从数据库中填充了每个视图。一切正常,直到我必须从collection_view_2
更新collection_view_1
中的视图。我正在做的是像以前在同一BehaviorRelay
中传递的那样传递ParentView
获取的数据。但是我看到的是这次是在collection_view_1's
单元的点击下,Observable
的{{1}}没有被调用。我无法弄清楚为什么这样做。我看到了其他SO问题,但没有人解决此问题。看看下面的代码,以便更好地理解。
Collection_view_1
StudentCell
Collection_view_2
class ClassTabCV: UIViewController, UICollectionViewDelegateFlowLayout {
let classCells = BehaviorRelay<[ClassModel]>(value: [])
let pickupVC = PickUpViewController()
override func viewDidLoad() {
super.viewDidLoad()
classTab.rx.setDelegate(self).disposed(by: disposeBag)
setupBinding()
}
func setupBinding() {
classTab.register(UINib(nibName: "ClassTabCVCell", bundle: nil), forCellWithReuseIdentifier: "classTabCV")
//Cell creation
classCells.asObservable().debug("Class View: ").bind(to: classTab.rx.items(cellIdentifier: "classTabCV", cellType: ClassTabCVCell.self)) {[weak self]
(row , element, cell) in
cell.viewModel = element
}.disposed(by: disposeBag)
// item selection with model details.
Observable
.zip(
classTab
.rx
.itemSelected,
classTab
.rx
.modelSelected(ClassModel.self))
.bind { [weak self] indexPath, model in
self?.pickupVC.getStudentsData2(id: model.classId) // here I am trying to pass reference to update Collection_view_2 views.
}.disposed(by: disposeBag)
}
}
ParentView_Controller
class StudentCV: UIViewController, UICollectionViewDelegateFlowLayout {
let studentCells = BehaviorRelay<[StudentModel]>(value: [])
var dataSource = RxCollectionViewSectionedAnimatedDataSource<SectionViewModel>(configureCell: {( _,_,_,_ ) in
fatalError()
})
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
studentsView.rx.setDelegate(self).disposed(by: disposeBag)
setupBinding()
}
func setupBinding() {
dataSource.configureCell = { (ds, cv, ip, item) in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "studentCV", for: ip) as! StudentCVCell
cell.viewModel = item
return cell
}
studentCells
.asObservable()
.debug("Student View: ")
.map({ [SectionViewModel(header: "", items: $0.filter({$0.pickupStatus == 2}) ), SectionViewModel(header: "", items: $0.filter({$0.pickupStatus == 3}) )] })
.bind(to: studentsView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
}
点击 let studentCells = BehaviorRelay<[StudentModel]>(value: [])
let classCells = BehaviorRelay<[ClassModel]>(value: [])
var studentCell : Observable<[StudentModel]> {
return studentCells.asObservable().debug("IS DATA IN: ")
} // this observable is not calling the time I pass reference from Collection_View_1 on tap.
var classCell : Observable<[ClassModel]> {
return classCells.asObservable()
}
override func viewDidLoad() {
super.viewDidLoad()
getAssignedClassData(classId: id)
self.getStudentsData(id: id)
bindViewModel()
}
func getStudentsData(id: Int) {
let studentsData = Database.singleton.fetchStudents(byCLassId: id)
self!.studentCells.accept(Array(studentsData))
let classData = Database.singleton.fetchClass()
self!.classCells.accept(Array(classData))
}
func getStudentsData2(id: Int) {
let studentsData = Database.singleton.fetchStudents(byCLassId: id)
self!.studentCells.accept(Array(studentsData)) // when this calls, observer is not emitting anything.
}
func bindViewModel() {
self
.studentCell
.asObservable()
.debug("BIND: ")
.observeOn(MainScheduler.instance)
.bind(to: studentsViewController.studentCells)
.disposed(by: disposeBag)
self
.classCell
.asObservable()
.debug("CELL BIND: ")
.observeOn(MainScheduler.instance)
.bind(to: classViewController.classCells)
.disposed(by: disposeBag)
}
。 collection_view_1
应该更新数据。并相应地更改视图。
在我看来,我正在做同样的事情,但不同之处是。 Collection_view_2
正在从collectionView
获得变更参考,而不是从任何其他CollectionView获得变更参考。任何帮助我解决问题的帮助将不胜感激。谢谢