Post

RxDataSource의 데이터 바인딩 과정

RxDataSource의 데이터 바인딩 과정

1. RxTableViewSecionReloadDataSource 생성

  • RxDataSource는 데이터의 섹션과 셀을 정의하는 클로저를 통해 UITableView를 관리합니다.
1
2
3
4
5
6
7
8
9
10
let dataSource = RxTableViewSectionReloadDataSource<SectionModel<String, String>>(
	configureCell: { dataSource, tableView, indexPath, item in
		let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
		cell.textLabel?.text = item //cell에 대한 설정
		return cell
	},
	titleForHeaderInsection: { dataSource, index in
		return dataSource.sectionModels[index].model
	}
)
  • configureCell: 각 섹션의 을 어떻게 그릴지 정의합니다.
  • titleForHeaderInSection: 각 섹션의 헤더를 어떻게 표시할지 정의합니다.

2. 데이터 준비

  • RxDataSource는 Observable<[SectionModel]> 데이터 필요
1
2
3
4
let sections = Observable.just([
    SectionModel(model: "오늘 할 일", items: ["할 일 1", "할 일 2"]),
    SectionModel(model: "내일 할 일", items: ["할 일 3", "할 일 4"])
])

3. 데이터와 UITableView 연결

  • RxDataSource와 UITableView를 연결하여 데이터 변경 시 UI가 자동으로 업데이트되도록 설정
1
2
3
4
5
6
7
8
9
override func viewDidLoad() {
    super.viewDidLoad()
    
    // 데이터 바인딩
    sections
        .bind(to: tableView.rx.items(dataSource: dataSource))
        .disposed(by: disposeBag)
}

This post is licensed under CC BY 4.0 by the author.