Created
August 21, 2018 02:20
-
-
Save danielt1263/873e190a039d0ed6edeba2c1693ea196 to your computer and use it in GitHub Desktop.
Rx DataSource custom implementation example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Daniel Tartaglia on 4/20/17. | |
// Copyright © 2017 Daniel Tartaglia. MIT License. | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
class ViewController: UIViewController { | |
@IBOutlet weak var tableView: UITableView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let items = Observable.just(["1", "2", "3"]) | |
items.bind(to: tableView.rx.items(dataSource: DataSource())) | |
.disposed(by: bag) | |
} | |
private let bag = DisposeBag() | |
} | |
class DataSource: NSObject, RxTableViewDataSourceType, UITableViewDataSource { | |
typealias Element = [String] | |
func tableView(_ tableView: UITableView, observedEvent event: Event<[String]>) { | |
if case let .next(items) = event { | |
self.items = items | |
tableView.reloadData() | |
} | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return items.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) | |
cell.textLabel?.text = items[indexPath.row] | |
return cell | |
} | |
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return "Header" | |
} | |
private var items: Element = [] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment