RxRealmDataSources icon indicating copy to clipboard operation
RxRealmDataSources copied to clipboard

UITableViewCell containing a UICollectionView

Open karta88821 opened this issue 7 years ago • 0 comments

Now I'm want to put UICollectionView into UITableViewCell using RxRealmDataSources library, and I use MVVM+RxSwift model,I know that the general approach is to set the collectionview dataSource and delegate in the tableviewcell to the ViewController itself, and then implement its protocal, but I don't know how to set the datasource of the collectionView using RxRealmDataSources. Do I need to add parameters in the ViewModel or?

In the ViewModel

class ViewModel {

    // MARK: - Output

    let boxs: Observable<(AnyRealmCollection<RMBox>,RealmChangeset?)>
    let cases: Observable<(AnyRealmCollection<RMCase>,RealmChangeset?)>

    init() {
        let boxResults = realm.objects(RMBox.self).sorted(byKeyPath: "id", ascending: true)
        self.boxs = Observable.changeset(from: boxResults).share()

        let caseResults = realm.objects(RMCase.self).sorted(byKeyPath: "id", ascending: true)
        self.cases = Observable.changeset(from: caseResults).share()
    }

}

In the ViewController

class ViewController: UIViewController {

    // Setup tableview dataSourceSetup collectionView dataSource(inside the tableView)
    let tbDataSource = RxTableViewRealmDataSource<RMBox>(
    cellIdentifier: CellId.main.rawValue, cellType: MainTableViewCell.self) {cell, ip, box in
        cell.setName(box.name)
        cell.collectionView.rx..... // How do I need to set for dataSource?
    }

       // Setup collectionView dataSource(inside the tableView)
       let clDataSource = RxCollectionViewRealmDataSource<RMCase>(
    cellIdentifier: CellId.main_collectionView.rawValue, cellType: MainCollectionViewCell.self) {cell, ip, _case in
        cell.setCaseName(_case.name)
    }


        override func viewDidLoad() {
        super.viewDidLoad()

        // Bind realm Results to tableView
        viewModel.boxs
            .observeOn(MainScheduler.instance)
            .bind(to: tableView.rx.realmChanges(tbDataSource))
            .disposed(by: disposeBag)

       // Bind realm Results to collectionView(inside tableViewcell)
       viewModel.cases
            .observeOn(MainScheduler.instance)
            .......
    }

}

karta88821 avatar Mar 10 '18 09:03 karta88821