From e20ac5fbb78f830779144cbc0e13da699eb26780 Mon Sep 17 00:00:00 2001 From: Krunoslav Zaher Date: Mon, 4 Jul 2016 01:42:26 +0200 Subject: [PATCH] Adds simple data source usage examples in inline docs. --- RxCocoa/iOS/UICollectionView+Rx.swift | 63 +++++++++++++++++++ RxCocoa/iOS/UITableView+Rx.swift | 63 +++++++++++++++++++ ...leViewExampleSectionedViewController.swift | 2 +- 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/RxCocoa/iOS/UICollectionView+Rx.swift b/RxCocoa/iOS/UICollectionView+Rx.swift index 5222c8d4..4fee4d70 100644 --- a/RxCocoa/iOS/UICollectionView+Rx.swift +++ b/RxCocoa/iOS/UICollectionView+Rx.swift @@ -24,6 +24,23 @@ extension UICollectionView { - parameter source: Observable sequence of items. - parameter cellFactory: Transform between sequence elements and view cells. - returns: Disposable object that can be used to unbind. + + Example + + let items = Observable.just([ + 1, + 2, + 3 + ]) + + items + .bindTo(collectionView.rx_itemsWithCellFactory) { (collectionView, row, element) in + let indexPath = NSIndexPath(forItem: row, inSection: 0) + let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell + cell.value?.text = "\(element) @ \(row)" + return cell + } + .addDisposableTo(disposeBag) */ public func rx_itemsWithCellFactory (source: O) @@ -44,6 +61,20 @@ extension UICollectionView { - parameter configureCell: Transform between sequence elements and view cells. - parameter cellType: Type of table view cell. - returns: Disposable object that can be used to unbind. + + Example + + let items = Observable.just([ + 1, + 2, + 3 + ]) + + items + .bindTo(collectionView.rx_itemsWithCellIdentifier("Cell", cellType: NumberCell.self)) { (row, element, cell) in + cell.value?.text = "\(element) @ \(row)" + } + .addDisposableTo(disposeBag) */ public func rx_itemsWithCellIdentifier (cellIdentifier: String, cellType: Cell.Type = Cell.self) @@ -70,6 +101,38 @@ extension UICollectionView { - parameter dataSource: Data source used to transform elements to view cells. - parameter source: Observable sequence of items. - returns: Disposable object that can be used to unbind. + + Example + + let dataSource = RxCollectionViewSectionedReloadDataSource>() + + let items = Observable.just([ + SectionModel(model: "First section", items: [ + 1.0, + 2.0, + 3.0 + ]), + SectionModel(model: "Second section", items: [ + 1.0, + 2.0, + 3.0 + ]), + SectionModel(model: "Third section", items: [ + 1.0, + 2.0, + 3.0 + ]) + ]) + + dataSource.configureCell = { (dataSource, cv, indexPath, element) in + let cell = cv.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell + cell.value?.text = "\(element) @ row \(indexPath.row)" + return cell + } + + items + .bindTo(collectionView.rx_itemsWithDataSource(dataSource)) + .addDisposableTo(disposeBag) */ public func rx_itemsWithDataSource< DataSource: protocol, diff --git a/RxCocoa/iOS/UITableView+Rx.swift b/RxCocoa/iOS/UITableView+Rx.swift index 4b64d484..cd4e6f18 100644 --- a/RxCocoa/iOS/UITableView+Rx.swift +++ b/RxCocoa/iOS/UITableView+Rx.swift @@ -24,6 +24,23 @@ extension UITableView { - parameter source: Observable sequence of items. - parameter cellFactory: Transform between sequence elements and view cells. - returns: Disposable object that can be used to unbind. + + Example: + + let items = Observable.just([ + "First Item", + "Second Item", + "Third Item" + ]) + + items + .bindTo(tableView.rx_itemsWithCellFactory) { (tableView, row, element) in + let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! + cell.textLabel?.text = "\(element) @ row \(row)" + return cell + } + .addDisposableTo(disposeBag) + */ public func rx_itemsWithCellFactory (source: O) @@ -44,6 +61,20 @@ extension UITableView { - parameter configureCell: Transform between sequence elements and view cells. - parameter cellType: Type of table view cell. - returns: Disposable object that can be used to unbind. + + Example: + + let items = Observable.just([ + "First Item", + "Second Item", + "Third Item" + ]) + + items + .bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) { (row, element, cell) in + cell.textLabel?.text = "\(element) @ row \(row)" + } + .addDisposableTo(disposeBag) */ public func rx_itemsWithCellIdentifier (cellIdentifier: String, cellType: Cell.Type = Cell.self) @@ -73,6 +104,38 @@ extension UITableView { - parameter dataSource: Data source used to transform elements to view cells. - parameter source: Observable sequence of items. - returns: Disposable object that can be used to unbind. + + Example + + let dataSource = RxTableViewSectionedReloadDataSource>() + + let items = Observable.just([ + SectionModel(model: "First section", items: [ + 1.0, + 2.0, + 3.0 + ]), + SectionModel(model: "Second section", items: [ + 1.0, + 2.0, + 3.0 + ]), + SectionModel(model: "Third section", items: [ + 1.0, + 2.0, + 3.0 + ]) + ]) + + dataSource.configureCell = { (dataSource, tv, indexPath, element) in + let cell = tv.dequeueReusableCellWithIdentifier("Cell")! + cell.textLabel?.text = "\(element) @ row \(indexPath.row)" + return cell + } + + items + .bindTo(tableView.rx_itemsWithDataSource(dataSource)) + .addDisposableTo(disposeBag) */ public func rx_itemsWithDataSource< DataSource: protocol, diff --git a/RxExample/RxExample/Examples/SimpleTableViewExampleSectioned/SimpleTableViewExampleSectionedViewController.swift b/RxExample/RxExample/Examples/SimpleTableViewExampleSectioned/SimpleTableViewExampleSectionedViewController.swift index f9e64f3b..c16f9cb5 100644 --- a/RxExample/RxExample/Examples/SimpleTableViewExampleSectioned/SimpleTableViewExampleSectionedViewController.swift +++ b/RxExample/RxExample/Examples/SimpleTableViewExampleSectioned/SimpleTableViewExampleSectionedViewController.swift @@ -36,7 +36,7 @@ class SimpleTableViewExampleSectionedViewController 2.0, 3.0 ]), - SectionModel(model: "Second section", items: [ + SectionModel(model: "Third section", items: [ 1.0, 2.0, 3.0