You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provides a reusable object to handle the boiler plate logic for the UITableViewDataSource.
9
+
SKTableViewDataSource provides an object to handle much of the standard UITableViewDataSource logic. It handles calculating row and section counts, retrieving cells, and provides methods for updating the underlying array powering the data source. Check out the SampleProject in the workspace to see some usage examples.
10
+
11
+
---
12
+
13
+
## Installation
14
+
15
+
### Cocoapods
16
+
17
+
Instalation is supported through Cocoapods. Add the following to your pod file for the target where you would like to use SKTableViewDataSource:
18
+
19
+
```
20
+
pod 'SKTableViewDataSource'
21
+
```
22
+
23
+
---
24
+
25
+
## Initialization
26
+
27
+
### Auto Cell Registration
28
+
29
+
The easiest way to initialize a TableViewDataSource object is to provide an array, cell class or nib, and a CellPresenter closure to handle styling the cell during `cellForRowAtIndexPath`. Cell registration will be handled by the `TableViewDataSource` object. The objects array can be a 1 or 2 dimensional array. A single dimension array will display as a single section table view. A 2 dimensional array will display with multiple sections.
30
+
31
+
32
+
```
33
+
import SKTableViewDataSource
34
+
```
35
+
36
+
```
37
+
let dataSource = TableViewDataSource(objects: array, cell: UITableViewCell.self, cellPresenter: { (cell, object) in
38
+
cell.textLabel?.text = object
39
+
})
40
+
41
+
tableview.dataSource = dataSource
42
+
```
43
+
44
+
### Manual Cell Registration
45
+
46
+
If you require access to the cell's reuse identifier or require multiple cell types in your table view, you can choose to register the cells yourself.
let dataSource = TableViewDataSource(objects: array, delegate: self) {
56
+
cell.textLabel?.text = object.rawValue
57
+
})
58
+
59
+
tableView.dataSource = dataSource
60
+
```
61
+
Note: If you choose to handle cell registration on your own, you must implement TableViewDataSourceDelegate's `cellForRowAtIndexPath` method and return a cell for each index path.
62
+
63
+
---
64
+
65
+
## CellPresenter
66
+
67
+
Each initialization method for auto cell registration has an optional `CellPresenter` closure. The closure returns two parameters: a cell and an object. This closure can be used to populate the cell with values from the object.
68
+
69
+
---
70
+
71
+
## TableViewDataSourceDelegate
72
+
73
+
`TableViewDataSource` has an optional delegate. This serves as a pass through for `UITableViewDataSource` methods. The delegate object can override any of the `TableViewDataSource` implementations by implementing the corresponding delegate method.
74
+
75
+
---
76
+
77
+
## Updating The Data Array
78
+
79
+
There are a handful of methods for manipulating the data in the array. Updating the data source will not trigger any sort of update in the table view. That must be handled by the developer.
80
+
81
+
### delete(indexPath:)
82
+
This will delete the object at the provided index path.
83
+
84
+
### insert(indexPath:)
85
+
This will insert the provided object at the provided index path.
86
+
87
+
### moveFrom(_:to:)
88
+
This will move the object at the from index path to the to index path.
89
+
90
+
### object(indexPath:)
91
+
This returns the object at the provided index path.
92
+
93
+
### setObjects(_:)
94
+
This replaces the existing objects array with the provided objects array.
0 commit comments