|
2 | 2 |
|
3 | 3 | A framework for `UICollectionView` and `UITableView` that provides: |
4 | 4 |
|
5 | | -- Advanced collection and table view diffable data sources with handlers for selecting, deleting, reordering, focusing and editing cells and additional functionality. |
| 5 | +- Extended collection and table view diffable data sources with handlers for selecting, reordering, focusing and editing cells and additional functionality. |
6 | 6 | - Table view cell registration similar to `UICollectionView.CellRegistration`. |
7 | 7 | - Additional extensions for `UICollectionView` and `UITableView`. |
| 8 | + |
| 9 | +## UICollectionViewDiffableDataSource & TableViewDiffableDataSource |
| 10 | + |
| 11 | +It provides handlers for selecting, reordering, focusing and editing cells and additional functionality. |
| 12 | + |
| 13 | +Examples of some of the included handlers: |
| 14 | + |
| 15 | +```swift |
| 16 | +/// Handler that gets called when the user did select an item. |
| 17 | +diffableDataSource.selectionHandlers.didSelect = { itemIdentifier in |
| 18 | + // did select an item |
| 19 | +} |
| 20 | + |
| 21 | +/// Handler that gets called when an item/cell is about to be shown. |
| 22 | +diffableDataSource.displayingHandlers.willDisplay = { itemIdentifier, cell in |
| 23 | + |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +`TableViewDiffableDataSource` provides handlers for reordering of cells/items like `UICollectionViewDiffableDataSource’s` reordering handlers: |
| 28 | + |
| 29 | +```swift |
| 30 | +// Allow every item to be reordered |
| 31 | +tableViewDataSource.reorderingHandlers.canReorder = { item in return true } |
| 32 | + |
| 33 | +// Update the backing store from the did reorder transaction. |
| 34 | +tableViewDataSource.reorderingHandlers.didReorder = { [weak self] transaction, itemIdentifier in |
| 35 | + guard let self = self else { return } |
| 36 | + |
| 37 | + if let updatedBackingStore = self.backingStore.applying(transaction.difference) { |
| 38 | + self.backingStore = updatedBackingStore |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## UITableView Cell and Header/Footer registration |
| 44 | + |
| 45 | +A registration for the table view’s cells and header/footer views similar to `UICollectionView.CellRegistration`. |
| 46 | + |
| 47 | +Use a cell registration to register table cell views with your table view and configure each cell for display. |
| 48 | + |
| 49 | +The following example creates a cell registration for cells of type `UITableViewCell`. Each cells textfield displays its item. |
| 50 | + |
| 51 | +```swift |
| 52 | +let cellRegistration = UITableView.CellRegistration<UITableViewCell, String> { cell, indexPath, string in |
| 53 | + var contentConfiguration = cell.defaultContentConfiguration() |
| 54 | + |
| 55 | + contentConfiguration.text = string |
| 56 | + contentConfiguration.textProperties.color = .lightGray |
| 57 | + |
| 58 | + cell.contentConfiguration = contentConfiguration |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +After you create a cell registration, you pass it in to ``UIKit/UITableView/dequeueConfiguredReusableCell(using:for:item:)``, which you call from your data source’s cell provider. |
| 63 | + |
| 64 | +```swift |
| 65 | +dataSource = UITableViewDiffableDataSource<Section, String>(tableView: tableView) { |
| 66 | + tableView, indexPath, item in |
| 67 | + return tableView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item) |
| 68 | +} |
| 69 | +``` |
0 commit comments