Item 을 가진 UICollectionView.SupplementaryRegistration
iOS14
부터 지원하는 UICollectionView
API 중 struct CellRegistration<Cell, Item> where Cell : UICollectionViewCell
의 경우 Cell
과 Item
을 전달하게 되는데 struct SupplementaryRegistration<Supplementary> where Supplementary : UICollectionReusableView
의 경우 Supplementary
만 전달하게 된다. 아래와 같이 확장하면 Supplementary
와 Item
을 전달 할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
extension UICollectionView {
public struct SupplementaryRegistrationWithItem<Supplementary, Item> where Supplementary: UICollectionReusableView {
public typealias Handler = (
Supplementary,
String,
IndexPath,
Item
) -> Void
public let elementKind: String
public let handler: Handler
public let supplementaryRegistration: SupplementaryRegistration<Supplementary>
public init(
elementKind: String,
handler: @escaping Handler
) {
self.elementKind = elementKind
self.handler = handler
self.supplementaryRegistration = .init(elementKind: elementKind, handler: { _, _, _ in })
}
}
/// Dequeues a configured reusable supplementary view object.
/// - Parameters:
/// - registration: The supplementary registration for configuring the supplementary view object. See UICollectionView.SupplementaryRegistration.
/// - indexPath: The index path that specifies the location of the supplementary view in the collection view.
/// - item: The item that provides data for the supplementary.
@MainActor
func dequeueConfiguredReusableSupplementary<Supplementary, Item>(
using registration: SupplementaryRegistrationWithItem<Supplementary, Item>,
for indexPath: IndexPath,
item: Item
) -> Supplementary where Supplementary: UICollectionReusableView {
let supplementaryView = self.dequeueConfiguredReusableSupplementary(
using: registration.supplementaryRegistration,
for: indexPath
)
registration.handler(
supplementaryView,
registration.elementKind,
indexPath,
item
)
return supplementaryView
}
}
참고
UICollectionView.CellRegistration
UICollectionView.SupplementaryRegistration
This post is licensed under CC BY 4.0 by the author.