Skip to content

Commit ede8588

Browse files
authored
Merge pull request #49 from plm75/documentation
added documentation for RxDataSources
2 parents a57864e + bbdf014 commit ede8588

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

README.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,109 @@ Table and Collection view data sources
2121
- [x] Works with `UITableView` and `UICollectionView`
2222

2323
## Why
24-
2524
Writing table and collection view data sources is tedious. There is a large number of delegate methods that need to be implemented for the simplest case possible.
2625

27-
The problem is even bigger when table view or collection view needs to display animated updates.
26+
RxSwift helps alleviate some of the burden with a simple data binding mechanism:
27+
1) Turn your data into an Observable stream
28+
2) Bind the data to the tableView/collectionView using one of:
29+
- `rx_itemsWithDataSource(:protocol<RxTableViewDataSourceType, UITableViewDataSource>)`
30+
- `rx_itemsWithCellIdentifier(:String)`
31+
- `rx_itemsWithCellIdentifier(:String:Cell.Type)`
32+
- `rx_itemsWithCellFactory(:ObservableType)`
33+
34+
```swift
35+
let dataSource = Observable<[String]>.just(["first element", "second element", "third element"])
36+
37+
dataSource.bindTo(tableView.rx_itemsWithCellIdentifier("Cell")) { index, model, cell in
38+
cell.textLabel?.text = model
39+
}
40+
.addDisposableTo(disposeBag)
41+
```
42+
43+
This works well with simple data sets but does not handle cases where you need to bind complex data sets with multiples sections, or when you need to perform animations when adding/modifying/deleting items.
44+
45+
These are precisely the use cases that RxDataSources helps solve.
2846

29-
This project makes it super easy to just write
47+
With RxDataSources, it is super easy to just write
3048

3149
```swift
3250
Observable.just([MySection(header: "title", items: [1, 2, 3])])
3351
.bindTo(tableView.rx_itemsWithDataSource(dataSource))
3452
.addDisposableTo(disposeBag)
3553
```
36-
3754
![RxDataSources example app](https://raw.githubusercontent.com/kzaher/rxswiftcontent/rxdatasources/RxDataSources.gif)
3855

56+
## How
57+
Given the following custom data structure:
58+
```swift
59+
struct CustomData {
60+
var anInt: Int
61+
var aString: String
62+
var aCGPoint: CGPoint
63+
}
64+
```
65+
66+
1) Start by defining your sections with a struct that conforms to the `SectionModelType` protocol:
67+
- define the `Item` typealias: equal to the type of items that the section will contain
68+
- declare an `items` property: of type array of `Item`
69+
70+
```swift
71+
struct SectionOfCustomData {
72+
var header: String
73+
var items: [Item]
74+
}
75+
extension SectionOfCustomData: SectionModelType {
76+
typealias Item = CustomData
77+
78+
init(original: SectionOfCustomData, items: [Item]) {
79+
self = original
80+
self.items = items
81+
}
82+
}
83+
```
84+
85+
2) Create a dataSource object and pass it your `SectionOfCustomData` type:
86+
```swift
87+
let dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>()
88+
```
89+
90+
3) Customize closures on the dataSource as needed:
91+
- `configureCell` (required)
92+
- `titleForHeaderInSection`
93+
- `titleForFooterInSection`
94+
- etc
95+
96+
```swift
97+
dataSource.configureCell = { ds, tv, ip, item in
98+
let cell = tv.dequeueReusableCellWithIdentifier("Cell", forIndexPath: ip)
99+
cell.textLabel?.text = "Item \(item.anInt): \(item.aString) - \(item.aCGPoint.x):\(item.aCGPoint.y)"
100+
return cell
101+
}
102+
dataSource.titleForHeaderInSection = { ds, index in
103+
return ds.sectionModels[index].header
104+
}
105+
```
106+
107+
4) Define the actual data as an Observable stream of CustomData objects and bind it to the tableView
108+
```swift
109+
let sections = [
110+
SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
111+
SectionOfCustomData(header: "Second section", items: [CustomData(anInt: 2, aString: "two", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 3, aString: "three", aCGPoint: CGPoint(x: 3, y: 3)) ])
112+
]
113+
114+
Observable.just(sections)
115+
.bindTo(tableView.rx_itemsWithDataSource(dataSource))
116+
.addDisposableTo(disposeBag)
117+
```
118+
119+
120+
### Animations
121+
To implement animations with RxDataSources, the same steps are required as with non-animated data, execept:
122+
- SectionOfCustomData needs to conform to `AnimatableSectionModelType`
123+
- dataSource needs to be an instance of `RxTableViewSectionedAnimatedDataSource` or `RxTableViewSectionedAniamtedDataSource`
124+
125+
126+
39127
## Installation
40128

41129
**We'll try to keep the API as stable as possible, but breaking API changes can occur.**

0 commit comments

Comments
 (0)