RxCocoa

  • 普通tableView数据绑定
        let dataOB = BehaviorSubject.init(value: self.viewModel.dataArray)

        // 方法一
        dataOB.asObserver().bind(to: self.tableView.rx.items(cellIdentifier: resuseID, cellType: LGTableViewCell.self)) { (row, model, cell) in
            cell.setUIData(model as! LGModel)
        }.disposed(by: self.disposeBag)
        
        // 方法二
//        dataOB.asObserver().asDriver(onErrorJustReturn: []).drive(self.tableView.rx.items) {
//            (tb, indexPath, model) -> LGTableViewCell in
//            let cell = tb.dequeueReusableCell(withIdentifier: self.resuseID) as! LGTableViewCell
//            cell.setUIData(model as! LGModel)
//            return cell
//        }.disposed(by: self.disposeBag)
  • tableView点击事件
        tableView.rx.itemSelected.subscribe(onNext: { [weak self](indexPath) in
            print("点击\(indexPath)行")
            self?.navigationController?.pushViewController(LGSectionViewController(), animated: true)
            self?.tableView.deselectRow(at: indexPath, animated: true)
        }).disposed(by: self.disposeBag)

methodInvoked 响应之后
sentMessage 响应之前

  • tableView复选点击事件
        tableView.rx.itemDeselected.subscribe(onNext: { (indexPath) in
            print("再次点击\(indexPath)行")
        }).disposed(by: self.disposeBag)
  • tableView移动事件
      tableView.rx.itemMoved.subscribe(onNext: { [weak self] (sourceIndex,destinationIndex) in
            print("从\(sourceIndex)移动到\(destinationIndex)")
            self?.viewModel.dataArray.swapAt(sourceIndex.row, destinationIndex.row)
            self?.loadUI(obSubject: dataOB)
        }).disposed(by: self.disposeBag)
  • tableView删除事件
        tableView.rx.itemDeleted.subscribe(onNext: { [weak self](indexPath) in
            print("点击删除\(indexPath)行")
            self?.viewModel.dataArray.remove(at: indexPath.row)
            self?.loadUI(obSubject: dataOB)
        }).disposed(by: self.disposeBag)
  • tableView新增事件
        tableView.rx.itemInserted.subscribe(onNext: { [weak self](indexPath) in
            print("添加数据在\(indexPath)行")
            guard let model = self?.viewModel.dataArray.last else{
                print("数据有问题,无法新增")
                return
            }
            self?.viewModel.dataArray.insert(model, at: indexPath.row)
            self?.loadUI(obSubject: dataOB)
        }).disposed(by: self.disposeBag)
  • 分组数据绑定
        let tableViewDataSource = RxTableViewSectionedReloadDataSource>(configureCell: { [weak self](dataSource, tab, indexPath, model) -> LGTableViewCell in

            let cell = tab.dequeueReusableCell(withIdentifier: self?.resuseID ?? "resuseID_LGSectionViewController", for: indexPath) as! LGTableViewCell
            cell.setUISectionData(model)
            cell.selectionStyle = .none
            return cell
        },titleForHeaderInSection: { dataSource,index -> String in
            // print("数据:\(dataSource.sectionModels[index].identity)")
            return dataSource.sectionModels[index].model
        })
        self.data.githubData.asDriver(onErrorJustReturn: [])
            .drive(self.tableView.rx.items(dataSource: tableViewDataSource))
            .disposed(by: self.disposeBag)

1、RxTableViewSectionedReloadDataSource -> TableViewSectionedDataSource -> init
2、self.tableView.delegate = self: items -> self.delegate -> RxScrollViewDelegateProxy.proxy(for: base) -> self._setCurrentDelegate(proxy, to: object) -> setCurrentDelegate(castOptionalOrFatalError(delegate), to: object) -> object.delegate = delegate
3、self.tableView.dataSource = self: items -> ource.subscribeProxyDataSource(o... -> DelegateProxy.proxy(for: object) -> self._setCurrentDelegate(proxy, to: object) -> setCurrentDelegate(castOptionalOrFatalError(delegate), to: object) -> object.delegate = delegate
4、items -> 向上滑动 -> RxTableViewReactiveArrayDataSourceSequenceWrapper -> RxTableViewReactiveArrayDataSource -> _RxTableViewReactiveArrayDataSource

你可能感兴趣的:(RxCocoa)