Reusable+cell.swift
2.04 KB
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
//
// Reusable+cell.swift
// InterQR-Internship
//
// Created by Leyter on 09.06.2022.
//
import Foundation
import UIKit
protocol Reusable: AnyObject {
static var reuseIdentifier: String { get }
}
extension Reusable {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
// swiftlint:disable force_cast
extension UICollectionView {
func registerReusableCell<T: UICollectionViewCell>(_: T.Type) where T: Reusable {
self.register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T where T: Reusable {
return self.dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
}
func registerReusableSupplementaryView<T: UICollectionReusableView>(elementKind: String, _: T.Type) where T: Reusable {
self.register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableSupplementaryView<T: UICollectionReusableView>(ofKind elementKind: String, for indexPath: IndexPath) -> T where T: Reusable {
return self.dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
}
}
extension UITableView {
func registerReusableCell<T: UITableViewCell>(_: T.Type) where T: Reusable {
self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
}
func registerReusableView<T: UITableViewHeaderFooterView>(_: T.Type) where T: Reusable {
self.register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T where T: Reusable {
return self.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as! T
}
func dequeueReusableView<T: UITableViewHeaderFooterView>() -> T where T: Reusable {
return self.dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as! T
}
}