Showing
1 changed file
with
51 additions
and
1 deletions
1 | // | 1 | // |
2 | -// Reuseble + Cell.swift | 2 | +// Reusable + Cell.swift |
3 | // InterQR-Internship | 3 | // InterQR-Internship |
4 | // | 4 | // |
5 | // Created by Дмитрий Тимофеев on 19.06.2022. | 5 | // Created by Дмитрий Тимофеев on 19.06.2022. |
6 | // | 6 | // |
7 | 7 | ||
8 | import Foundation | 8 | import Foundation |
9 | +import UIKit | ||
10 | + | ||
11 | +protocol Reusable: AnyObject { | ||
12 | + static var reuseIdentifier: String { get } | ||
13 | +} | ||
14 | + | ||
15 | +extension Reusable { | ||
16 | + static var reuseIdentifier: String { | ||
17 | + return String(describing: Self.self) | ||
18 | + } | ||
19 | +} | ||
20 | + | ||
21 | +// swiftlint:disable force_cast | ||
22 | + | ||
23 | +extension UICollectionView { | ||
24 | + func registerReusableCell<T: UICollectionViewCell>(_: T.Type) where T: Reusable { | ||
25 | + self.register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier) | ||
26 | + } | ||
27 | + | ||
28 | + func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T where T: Reusable { | ||
29 | + return self.dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T | ||
30 | + } | ||
31 | + | ||
32 | + func registerReusableSupplementaryView<T: UICollectionReusableView>(elementKind: String, _: T.Type) where T: Reusable { | ||
33 | + self.register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier) | ||
34 | + } | ||
35 | + | ||
36 | + func dequeueReusableSupplementaryView<T: UICollectionReusableView>(ofKind elementKind: String, for indexPath: IndexPath) -> T where T: Reusable { | ||
37 | + return self.dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T | ||
38 | + } | ||
39 | +} | ||
40 | + | ||
41 | +extension UITableView { | ||
42 | + func registerReusableCell<T: UITableViewCell>(_: T.Type) where T: Reusable { | ||
43 | + self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier) | ||
44 | + } | ||
45 | + | ||
46 | + func registerReusableView<T: UITableViewHeaderFooterView>(_: T.Type) where T: Reusable { | ||
47 | + self.register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | ||
48 | + } | ||
49 | + | ||
50 | + func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T where T: Reusable { | ||
51 | + return self.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as! T | ||
52 | + } | ||
53 | + | ||
54 | + func dequeueReusableView<T: UITableViewHeaderFooterView>() -> T where T: Reusable { | ||
55 | + return self.dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as! T | ||
56 | + } | ||
57 | +} | ||
58 | + |
Please
register
or
login
to post a comment