-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UICollectionView+Cell.swift
49 lines (38 loc) 路 1.48 KB
/
UICollectionView+Cell.swift
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
//
// UICollectionView+Register.swift
// Sucrose
//
// Created by Pedro Carrasco on 14/02/2019.
// Copyright 漏 2019 Pedro Carrasco. All rights reserved.
//
import UIKit
public extension UICollectionView {
enum SupplementaryViewKind {
case header
case footer
var value: String {
switch self {
case .header: return UICollectionView.elementKindSectionHeader
case .footer: return UICollectionView.elementKindSectionFooter
}
}
}
func register<T: UICollectionViewCell>(_: T.Type) {
register(T.self, forCellWithReuseIdentifier: T.name)
}
func register<T: UICollectionReusableView>(_: T.Type, as kind: SupplementaryViewKind) {
register(T.self, forSupplementaryViewOfKind: kind.value, withReuseIdentifier: T.name)
}
func dequeue<T: UICollectionViewCell>(_ type: T.Type, in indexPath: IndexPath) -> T {
guard let cell = dequeueReusableCell(withReuseIdentifier: type.name, for: indexPath) as? T else {
fatalError("Unable to dequeue \(T.name)")
}
return cell
}
func dequeue<T: UICollectionReusableView>(_ type: T.Type, in indexPath: IndexPath, as kind: SupplementaryViewKind) -> T {
guard let view = dequeueReusableSupplementaryView(ofKind: kind.value, withReuseIdentifier: T.name, for: indexPath) as? T else {
fatalError("Unable to dequeue \(T.name)")
}
return view
}
}