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
|
//
// UITableView+ReuseIdentifier.swift
// MullvadVPN
//
// Created by pronebird on 09/11/2023.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
/**
Type describing cell identifier.
Example:
```
enum MyCellIdentifier: CaseIterable, String, CellIdentifierProtocol {
case primary, secondary
var cellClass: AnyClass {
switch self {
case .primary:
return MyPrimaryCell.self
case .secondary:
return MySecondaryCell.self
}
}
}
// Register all cells
tableView.registerReusableViews(from: MyCellIdentifier.self)
// Dequeue cell
let cell = tableView.dequeueReusableView(withIdentifier: MyCellIdentifier.primary, for: IndexPath(row: 0, section: 0)))
```
*/
protocol CellIdentifierProtocol: CaseIterable, RawRepresentable where RawValue == String {
var cellClass: AnyClass { get }
}
/**
Type describing header footer view identifier.
Example:
```
enum MyHeaderFooterIdentifier: CaseIterable, String, HeaderFooterIdentifierProtocol {
case primary, secondary
var headerFooterClass: AnyClass {
switch self {
case .primary:
return MyPrimaryHeaderFooterView.self
case .secondary:
return MySecondaryHeaderFooterView.self
}
}
}
// Register all cells
tableView.registerReusableViews(from: MyHeaderFooterIdentifier.self)
// Dequeue header footer view
let headerFooterView = tableView.dequeueReusableView(withIdentifier: MyHeaderFooterIdentifier.primary)
```
*/
protocol HeaderFooterIdentifierProtocol: CaseIterable, RawRepresentable where RawValue == String {
var headerFooterClass: AnyClass { get }
}
extension UITableView {
/// Register all cell identifiers in the table view.
/// - Parameter cellIdentifierType: a type conforming to the ``CellIdentifierProtocol`` protocol.
func registerReusableViews<T: CellIdentifierProtocol>(from cellIdentifierType: T.Type) {
cellIdentifierType.allCases.forEach { identifier in
register(identifier.cellClass, forCellReuseIdentifier: identifier.rawValue)
}
}
/// Register header footer view identifiers in the table view.
/// - Parameter headerFooterIdentifierType: a type conforming to the ``HeaderFooterIdentifierProtocol`` protocol.
func registerReusableViews<T: HeaderFooterIdentifierProtocol>(from headerFooterIdentifierType: T.Type) {
headerFooterIdentifierType.allCases.forEach { identifier in
register(identifier.headerFooterClass, forHeaderFooterViewReuseIdentifier: identifier.rawValue)
}
}
}
extension UITableView {
/// Convenience method to dequeue a cell by identifier conforming to ``CellIdentifierProtocol``.
/// - Parameters:
/// - identifier: cell identifier.
/// - indexPath: index path
/// - Returns: table cell.
func dequeueReusableView(
withIdentifier identifier: some CellIdentifierProtocol,
for indexPath: IndexPath
) -> UITableViewCell {
dequeueReusableCell(withIdentifier: identifier.rawValue, for: indexPath)
}
/// Convenience method to dequeue a header footer view by identifier conforming to ``HeaderFooterIdentifierProtocol``.
/// - Parameter identifier: header footer view identifier.
/// - Returns: table header footer view.
func dequeueReusableView(withIdentifier identifier: some HeaderFooterIdentifierProtocol)
-> UITableViewHeaderFooterView?
{
dequeueReusableHeaderFooterView(withIdentifier: identifier.rawValue)
}
}
|