Skip to content

Commit 6ed826d

Browse files
committed
updated readme
1 parent ab23a86 commit 6ed826d

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,68 @@
22

33
A framework for `UICollectionView` and `UITableView` that provides:
44

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.
66
- Table view cell registration similar to `UICollectionView.CellRegistration`.
77
- 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+
```

Sources/AdvancedCollectionTableView-iOS/TableView/DataSource/TableViewDiffableDataSource.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ class TableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : U
464464

465465
```swift
466466
// Allow every item to be reordered
467-
dataSource.reorderingHandlers.canDelete = { elements in return true }
467+
dataSource.reorderingHandlers.canReorder = { item in return true }
468468

469469
// Option 1: Update the backing store from a CollectionDifference
470-
dataSource.reorderingHandlers.didDelete = { [weak self] items, transaction in
470+
dataSource.reorderingHandlers.didReorder = { [weak self] transaction, itemIdentifier in
471471
guard let self = self else { return }
472472

473473
if let updatedBackingStore = self.backingStore.applying(transaction.difference) {
@@ -476,7 +476,7 @@ class TableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : U
476476
}
477477

478478
// Option 2: Update the backing store from the final items
479-
dataSource.reorderingHandlers.didReorder = { [weak self] items, transaction in
479+
dataSource.reorderingHandlers.didReorder = { [weak self] transaction, itemIdentifier in
480480
guard let self = self else { return }
481481

482482
self.backingStore = transaction.finalSnapshot.itemIdentifiers

0 commit comments

Comments
 (0)