Skip to content

Commit 662eaf9

Browse files
committed
updated
1 parent 33f4a0b commit 662eaf9

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ A framework for `UICollectionView` and `UITableView` that provides:
88

99
## UICollectionViewDiffableDataSource & TableViewDiffableDataSource
1010

11-
It provides handlers for selecting, reordering, focusing and editing cells and additional functionality.
11+
### Handlers
12+
13+
There are handlers for selecting, reordering, focusing and editing cells.
1214

1315
Examples of some of the included handlers:
1416

@@ -24,7 +26,7 @@ diffableDataSource.displayingHandlers.willDisplay = { itemIdentifier, cell in
2426
}
2527
```
2628

27-
`TableViewDiffableDataSource` provides handlers for reordering of cells/items like `UICollectionViewDiffableDataSource’s` reordering handlers:
29+
`TableViewDiffableDataSource` provides handlers for reordering of cells/items like the reordering handlers of `UICollectionViewDiffableDataSource` that Apple provides:
2830

2931
```swift
3032
// Allow every item to be reordered
@@ -40,6 +42,21 @@ tableViewDataSource.reorderingHandlers.didReorder = { [weak self] transaction, _
4042
}
4143
```
4244

45+
### Empty datasource view
46+
47+
`emptyCollectionView` and `emptyTableView` displays the provided view when the datasource doesn't contain any items:
48+
49+
```swift
50+
diffableDataSource.emptyTableView = myEmptyView
51+
```
52+
53+
Alternatively you can use `emptyContentConfiguration` and provide an `UIContentConfiguration`:
54+
55+
```swift
56+
let loadingConfiguration = UIContentUnavailableConfiguration.loading()
57+
diffableDataSource.emptyContentConfiguration = loadingConfiguration
58+
```
59+
4360
## UITableView Cell Registration
4461

4562
A registration for the table view’s cells similar to `UICollectionView.CellRegistration`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// ContentConfigurationView.swift
3+
//
4+
//
5+
// Created by Florian Zand on 21.02.24.
6+
//
7+
8+
import UIKit
9+
10+
class ContentConfigurationView: UIView {
11+
var contentView: (UIView & UIContentView)
12+
13+
var contentConfiguration: UIContentConfiguration {
14+
didSet {
15+
updateContentView()
16+
}
17+
}
18+
19+
func updateContentView() {
20+
contentView.removeFromSuperview()
21+
contentView = contentConfiguration.makeContentView()
22+
addSubview(withConstraint: contentView)
23+
}
24+
25+
init(configuration: UIContentConfiguration) {
26+
self.contentConfiguration = configuration
27+
self.contentView = configuration.makeContentView()
28+
super.init(frame: .zero)
29+
addSubview(withConstraint: contentView)
30+
}
31+
32+
required init?(coder: NSCoder) {
33+
fatalError("init(coder:) has not been implemented")
34+
}
35+
}

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,46 @@ class TableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : U
3636
}
3737
}
3838

39-
/// The view that is displayed when the datasource doesn't contain any items.
40-
open var emptyCollectionView: UIView? = nil {
39+
/**
40+
The view that is displayed when the datasource doesn't contain any items.
41+
42+
When using a empty table view, ``emptyContentConfiguration`` is set to `nil`.
43+
*/
44+
open var emptyTableView: UIView? = nil {
4145
didSet {
42-
guard oldValue != emptyCollectionView else { return }
46+
guard oldValue != emptyTableView else { return }
47+
if emptyTableView != nil {
48+
emptyContentConfiguration = nil
49+
}
4350
updateEmptyCollectionView()
4451
}
4552
}
4653

54+
/**
55+
The content configuration that content view is displayed when the datasource doesn't contain any items.
56+
57+
When using a content configuration, ``emptyTableView`` is set to `nil`.
58+
*/
59+
open var emptyContentConfiguration: UIContentConfiguration? = nil {
60+
didSet {
61+
if let configuration = emptyContentConfiguration {
62+
emptyTableView?.removeFromSuperview()
63+
emptyTableView = nil
64+
if let emptyContentView = self.emptyContentView {
65+
emptyContentView.contentConfiguration = configuration
66+
} else {
67+
emptyContentView = .init(configuration: configuration)
68+
}
69+
} else {
70+
emptyContentView?.removeFromSuperview()
71+
emptyContentView = nil
72+
}
73+
updateEmptyCollectionView()
74+
}
75+
}
76+
77+
var emptyContentView: ContentConfigurationView?
78+
4779
override func apply(_ snapshot: NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true, completion: (() -> Void)? = nil) {
4880
super.apply(snapshot, animatingDifferences: animatingDifferences, completion: completion)
4981
updateEmptyCollectionView()
@@ -58,9 +90,12 @@ class TableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : U
5890
func updateEmptyCollectionView() {
5991
let snapshot = snapshot()
6092
if !snapshot.itemIdentifiers.isEmpty && !snapshot.sectionIdentifiers.isEmpty {
61-
emptyCollectionView?.removeFromSuperview()
62-
} else if let emptyCollectionView = self.emptyCollectionView {
63-
tableView?.addSubview(withConstraint: emptyCollectionView)
93+
emptyTableView?.removeFromSuperview()
94+
emptyContentView?.removeFromSuperview()
95+
} else if let emptyTableView = self.emptyTableView, emptyTableView.superview != tableView {
96+
tableView?.addSubview(withConstraint: emptyTableView)
97+
} else if let emptyContentView = self.emptyContentView, emptyContentView.superview != tableView {
98+
tableView?.addSubview(withConstraint: emptyContentView)
6499
}
65100
}
66101

0 commit comments

Comments
 (0)