2

我有 AparentView持有两个不同的CollectionViwes. 每个视图都通过将数据传递给Observableusing 来从数据库中填充BehaviorRelay。一切正常,直到我必须collection_view_2collection_view_1. 我正在做的是BehaviorRelay像以前一样传递获取的数据ParentView。但是我看到的是这次点击collection_view_1's单元格ObservableStudentCell没有被调用。我无法弄清楚为什么它会那样做。我见过其他 SO 问题,但没有人有答案来解决这个问题。请查看下面的代码以更好地理解。

Collection_view_1

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)
    }
}

Collection_view_2

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)
    }
}

ParentView_Controller

    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_1Collection_view_2应该更新数据。并相应地改变观点。在我的另一个观点中,我正在做同样的事情,但不同的是。这不是从任何其他 CollectionViewcollectionView获取更改引用。buttonClick任何对我的问题进行评分的帮助将不胜感激。谢谢

4

0 回答 0