https://github.com/flocked/advancedcollectiontableview-ios
Extended UICollectionView, UITableView, Diffable Data Sources, Cells and more.
https://github.com/flocked/advancedcollectiontableview-ios
ios swift uicollectionview uicollectionviewcell uicollectionviewdiffabledatasource uitableview uitableviewcell uitableviewdiffabledatasource
Last synced: about 1 month ago
JSON representation
Extended UICollectionView, UITableView, Diffable Data Sources, Cells and more.
- Host: GitHub
- URL: https://github.com/flocked/advancedcollectiontableview-ios
- Owner: flocked
- Created: 2024-02-21T19:48:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-22T01:21:23.000Z (over 1 year ago)
- Last Synced: 2025-04-06T10:02:09.987Z (about 2 months ago)
- Topics: ios, swift, uicollectionview, uicollectionviewcell, uicollectionviewdiffabledatasource, uitableview, uitableviewcell, uitableviewdiffabledatasource
- Language: Swift
- Homepage:
- Size: 58.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Advanced UICollectionView & UITableView
A framework for `UICollectionView` and `UITableView` that provides:
- Extended collection and table view diffable data sources with handlers for selecting, reordering, focusing and editing cells and additional functionality.
- Table view cell registration similar to `UICollectionView.CellRegistration`.
- Additional extensions for `UICollectionView` and `UITableView`.## UICollectionViewDiffableDataSource & TableViewDiffableDataSource
### Handlers
There are handlers for selecting, reordering, focusing and editing cells.
Examples of some of the included handlers:
```swift
// Handler that gets called when the user did select an item.
diffableDataSource.selectionHandlers.didSelect = { itemIdentifier in}
// Handler that gets called when an item/cell is about to be shown.
diffableDataSource.displayingHandlers.willDisplay = { itemIdentifier, cell in}
````TableViewDiffableDataSource` provides handlers for reordering cells like the `UICollectionViewDiffableDataSource` reordering handlers that Apple provides:
```swift
// Allow every item to be reordered
tableViewDataSource.reorderingHandlers.canReorder = { item in return true }// Update the backing store from the did reorder transaction.
tableViewDataSource.reorderingHandlers.didReorder = { [weak self] transaction, _ in
guard let self = self else { return }
if let updatedCurrentItems = self.currentItems.applying(transaction.difference) {
self.currentItems = updatedCurrentItems
}
}
```### Empty datasource view
`emptyView` displays the provided view when the datasource doesn't contain any items:
```swift
diffableDataSource.emptyView = myEmptyView
```Alternatively you can use `emptyContentConfiguration` and provide an `UIContentConfiguration`:
```swift
let loadingConfiguration = UIContentUnavailableConfiguration.loading()
diffableDataSource.emptyContentConfiguration = loadingConfiguration
```## UITableView Cell Registration
A registration for the table view’s cells similar to `UICollectionView.CellRegistration`.
Use a cell registration to register table cell views with your table view and configure each cell for display.
The following example creates a cell registration for cells of type `UITableViewCell` and string items. Each cells textfield displays its item string.
```swift
let cellRegistration = UITableView.CellRegistration { cell, indexPath, string in
var contentConfiguration = cell.defaultContentConfiguration()contentConfiguration.text = string
contentConfiguration.textProperties.color = .lightGraycell.contentConfiguration = contentConfiguration
}
```After you create a cell registration, you pass it in to ``dequeueConfiguredReusableCell(using:for:item:)``, which you call from your data source’s cell provider.
```swift
dataSource = UITableViewDiffableDataSource(tableView: tableView) {
tableView, indexPath, item in
return tableView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
}
```Alternatively you can use the ``UICollectionViewDiffableDataSource`` and ``UITableViewDiffableDataSource`` initializers:
```swift
let dataSource = UITableViewDiffableDataSource(tableView: myTableView, cellRegistration: cellRegistration)
}
```## UITableView Section View Registration
A registration for the table view’s header/footer section views.
The following example creates a section view registration for views of type `UITableViewHeaderFooterView` for sections with strings. Each section header views default content configuration text displays its section string.
```swift
let sectionViewRegistration = UITableView.SectionViewRegistration {
sectionView, indexPath, string in
var configuration = sectionView.defaultContentConfiguration()
configuration.text = string
sectionView.contentConfiguration = configuration
}
```
After you create a section view registration, you pass it in to ``dequeueConfiguredReusableSectionView(using:section:)``, which you call from your data source’s section header view provider.```swift
dataSource.headerViewProvider = { tableView, section in
return tableView.dequeueConfiguredReusableSectionView(using: sectionViewRegistration, section: section)
}
```Alternatively you can also ``applyHeaderViewRegistration()`` and `applyFooterViewRegistration()``:
```swift
dataSource.applyHeaderViewRegistration(sectionViewRegistration)
```## Diffable DataSource Snapshot Apply Options
Options for applying snapshots to a diffable datasource:
```swift
// Applies the snapshot animated with default animation duration.
dataSource.apply(mySnapshot, .animated)// Applies the snapshot animated with the specified animation duration.
dataSource.apply(mySnapshot, .animated(duration: 2.0))// Applies the snapshot non animated.
dataSource.apply(mySnapshot, .withoutAnimation)// Applies the snapshot using reload data.
dataSource.apply(mySnapshot, .usingReloadData)
```