#8 Airports UITableView Binding using RxDatasources - RxSwift MVVM Coordinator iOS App
RxDataSources
를 통한 테이블 뷰 구현typealias AirportItemsSection = SectionModel<Int, AirportViewPresentable>
protocol AirportsViewPresentable {
typealias Input = (
)
typealias Output = (
title: Driver<String>,
airports: Driver<[AirportItemsSection]>
)
typealias Dependencies = (
title: String,
models: Set<AirportModel>
)
typealias ViewModelBuilder = (AirportsViewPresentable.Input) -> AirportsViewPresentable
var input: AirportsViewPresentable.Input { get }
var output: AirportsViewPresentable.Output { get }
}
AirportsViewPresentable
을 따르기 때문에 사용 가능override func start() {
let vc = AirportsViewController()
vc.viewModelBuilder = { [models] in
let title = models.first?.city ?? ""
return AirportsViewModel(input: $0, dependencies: (title: title, models: models))
}
self.navigationController.pushViewController(vc, animated: true)
}
AirportsCoordinator
에서 넣어줌private func configureDataSource() {
dataSource = .init(configureCell: { _, tableView, indexPath, item in
guard let cell = tableView.dequeueReusableCell(withIdentifier: AirportTableViewCell.identifier, for: indexPath) as? AirportTableViewCell else { fatalError() }
cell.configure(with: item)
return cell
})
}
RxDataSources
가 제공하는 섹션 모델을 커스텀, 해당 섹션 모델을 통해 데이터 소스를 구성private func bind() {
guard let dataSource = dataSource else { return }
viewModel?
.output
.airports
.drive(tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
viewModel?
.output
.title
.drive(rx.title)
.disposed(by: disposeBag)
}
dataSource
를 통해 테이블 뷰 UI 표현rx.title
은 뷰 컨트롤러 자체의 타이틀을 반응형으로 보여주는 듯
Coordinator
내에서 뷰 컨트롤러와 뷰 모델을 빌드, 연결하는 과정에 점점 더 익숙해지는 것 같긴 한데...