-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUITableView+Reusable.swift
114 lines (92 loc) · 5.08 KB
/
UITableView+Reusable.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import UIKit
// MARK: Reusable support for UITableView
public extension UITableView {
/**
Register a NIB-Based `UITableViewCell` subclass (conforming to `Reusable` & `NibLoadable`)
- parameter cellType: the `UITableViewCell` (`Reusable` & `NibLoadable`-conforming) subclass to register
- seealso: `register(_:,forCellReuseIdentifier:)`
*/
final func register<T: UITableViewCell>(cellType: T.Type)
where T: Reusable & NibLoadable {
self.register(cellType.nib, forCellReuseIdentifier: cellType.reuseIdentifier)
}
/**
Register a Class-Based `UITableViewCell` subclass (conforming to `Reusable`)
- parameter cellType: the `UITableViewCell` (`Reusable`-conforming) subclass to register
- seealso: `register(_:,forCellReuseIdentifier:)`
*/
final func register<T: UITableViewCell>(cellType: T.Type)
where T: Reusable {
self.register(cellType.self, forCellReuseIdentifier: cellType.reuseIdentifier)
}
/**
Returns a reusable `UITableViewCell` object for the class inferred by the return-type
- parameter indexPath: The index path specifying the location of the cell.
- parameter cellType: The cell class to dequeue
- returns: A `Reusable`, `UITableViewCell` instance
- note: The `cellType` parameter can generally be omitted and infered by the return type,
except when your type is in a variable and cannot be determined at compile time.
- seealso: `dequeueReusableCell(withIdentifier:,for:)`
*/
final func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath, cellType: T.Type = T.self) -> T
where T: Reusable {
guard let cell = self.dequeueReusableCell(withIdentifier: cellType.reuseIdentifier, for: indexPath) as? T else {
fatalError(
"Failed to dequeue a cell with identifier \(cellType.reuseIdentifier) matching type \(cellType.self). "
+ "Check that the reuseIdentifier is set properly in your XIB/Storyboard "
+ "and that you registered the cell beforehand"
)
}
return cell
}
final func dequeueReusableCell<T: UITableViewCell>(cellType: T.Type = T.self) -> T
where T: Reusable {
guard let cell = self.dequeueReusableCell(withIdentifier: cellType.reuseIdentifier) as? T else {
fatalError(
"Failed to dequeue a cell with identifier \(cellType.reuseIdentifier) matching type \(cellType.self). "
+ "Check that the reuseIdentifier is set properly in your XIB/Storyboard "
+ "and that you registered the cell beforehand"
)
}
return cell
}
/**
Register a NIB-Based `UITableViewHeaderFooterView` subclass (conforming to `Reusable` & `NibLoadable`)
- parameter headerFooterViewType: the `UITableViewHeaderFooterView` (`Reusable` & `NibLoadable`-conforming)
subclass to register
- seealso: `register(_:,forHeaderFooterViewReuseIdentifier:)`
*/
final func register<T: UITableViewHeaderFooterView>(headerFooterViewType: T.Type)
where T: Reusable & NibLoadable {
self.register(headerFooterViewType.nib, forHeaderFooterViewReuseIdentifier: headerFooterViewType.reuseIdentifier)
}
/**
Register a Class-Based `UITableViewHeaderFooterView` subclass (conforming to `Reusable`)
- parameter headerFooterViewType: the `UITableViewHeaderFooterView` (`Reusable`-confirming) subclass to register
- seealso: `register(_:,forHeaderFooterViewReuseIdentifier:)`
*/
final func register<T: UITableViewHeaderFooterView>(headerFooterViewType: T.Type)
where T: Reusable {
self.register(headerFooterViewType.self, forHeaderFooterViewReuseIdentifier: headerFooterViewType.reuseIdentifier)
}
/**
Returns a reusable `UITableViewHeaderFooterView` object for the class inferred by the return-type
- parameter viewType: The view class to dequeue
- returns: A `Reusable`, `UITableViewHeaderFooterView` instance
- note: The `viewType` parameter can generally be omitted and infered by the return type,
except when your type is in a variable and cannot be determined at compile time.
- seealso: `dequeueReusableHeaderFooterView(withIdentifier:)`
*/
final func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_ viewType: T.Type = T.self) -> T?
where T: Reusable {
guard let view = self.dequeueReusableHeaderFooterView(withIdentifier: viewType.reuseIdentifier) as? T? else {
fatalError(
"Failed to dequeue a header/footer with identifier \(viewType.reuseIdentifier) "
+ "matching type \(viewType.self). "
+ "Check that the reuseIdentifier is set properly in your XIB/Storyboard "
+ "and that you registered the header/footer beforehand"
)
}
return view
}
}