RxSwift学习

RxSwift<9> —— MVVM双向绑定

2019-10-18  本文已影响0人  Gollum_

RxSwift 遇到tableView

  let dataOB = BehaviorSubject.init(value: self.viewModel.dataArray)
//        dataOB.bind(to: self.tableView.rx.items){(table,row,model) -> UITableViewCell in
//            let cell = table.dequeueReusableCell(withIdentifier: self.resuseID)
//            return cell as! LGTableViewCell
//        }
        
        dataOB.asObserver().bind(to: self.tableView.rx.items(cellIdentifier:self.resuseID,cellType: LGTableViewCell.self)){(row,model,cell) in
            cell.setUIData(model as! LGModel)
        }.disposed(by: disposeBag)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{[weak self] index in
                
            }).disposed(by: disposeBag)
        
        tableView.rx.itemDeselected
            .subscribe(onNext:{index in
                
            }).disposed(by: disposeBag)
        
        tableView.rx.itemMoved
            .subscribe(onNext:{[weak self] (sourceIndex,destinationIndex) in
              print("从\(sourceIndex)移动到\(destinationIndex)")
            }).disposed(by: disposeBag)
extension LGRxViewController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
}

分组tableView

let tableViewDataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,LGSectionModel>>(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
    })

    /// 我们上次就是通过bind函数,这里我们变化一下
    self.data.githubData.asDriver(onErrorJustReturn: [])
        .drive(self.tableView.rx.items(dataSource: tableViewDataSource))
        .disposed(by: self.disposeBag)

MVVM双向绑定
VC中:

override func viewDidLoad() {
    super.viewDidLoad()
    self.setupUI()
    // 现在来一个需求:我们的输入框搜索 - 请求网络 - 下面tableView联动
    // UI <-> model
    self.searchBar.rx.text.orEmpty
        .bind(to: self.viewModel.searchTextOB)
        .disposed(by: disposeBag)
    
   // 数据层绑定UI
    self.viewModel.searchData.drive(self.tableView.rx.items){ (tableView, indexPath, model) -> LGTableViewCell in
        let cell = tableView.dequeueReusableCell(withIdentifier: self.resuseID) as! LGTableViewCell
        cell.nameLabel.text  = model.name
        cell.classLabel.text = model.url
        return cell
    }
    .disposed(by: disposeBag)
    
    // tittle绑定
    self.viewModel.searchData.map { (array) -> String in
        return "搜索到了 \(array.count) 条数据"
    }
    .drive(self.navigationItem.rx.title)
    .disposed(by: disposeBag)
    
    // 滑动减速绑定
    self.tableView.rx.didEndDecelerating
        .subscribe { [weak self] in
            self?.searchBar.endEditing(true)
    }.disposed(by: disposeBag)
}

VM中

let searchTextOB = BehaviorSubject(value: "")
lazy var searchData: Driver<[LGReposityModel]> = {
    return self.searchTextOB.asObserver()
        .throttle(RxTimeInterval.milliseconds(300), scheduler: MainScheduler.instance)
        .distinctUntilChanged()
        .flatMapFirst(LGComprehensiveViewModel.resposeData)
        .asDriver(onErrorJustReturn: [])
}()

或者直接用BehaviorRelay封装数据源,在VC中绑定tableview

上一篇 下一篇

猜你喜欢

热点阅读