From abfdc44a0ae7c6e9b0130277d7219861ff18842a Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Thu, 23 May 2019 12:36:41 +0200 Subject: [PATCH] #14 Fix for ReusableCellViewProtocol override --- .../Models & UI/CatalogItemCell.swift | 2 +- OwlDemo/Contacts/Models & UI/CellUser.swift | 5 ++ .../Models & UI/GroupFooterView.swift | 6 ++ .../Models & UI/GroupHeaderView.swift | 1 - OwlDemo/Emoji/UI/EmojiHeaderView.swift | 2 +- .../Cell Adapter/CollectionCellAdapter.swift | 4 +- .../CollectionHeaderFooterAdapter.swift | 8 +-- .../Protocols/ReusableCellViewProtocol.swift | 72 +++++++++++-------- .../Cell Adapters/TableCellAdapter.swift | 6 +- .../TableHeaderFooterAdapter.swift | 4 +- .../TableSectionHeaderFooterProtocol.swift | 4 +- Sources/Table/TableDirector.swift | 2 +- 12 files changed, 70 insertions(+), 46 deletions(-) diff --git a/OwlDemo/Catalog Menu/Models & UI/CatalogItemCell.swift b/OwlDemo/Catalog Menu/Models & UI/CatalogItemCell.swift index e25c0a1..2319168 100644 --- a/OwlDemo/Catalog Menu/Models & UI/CatalogItemCell.swift +++ b/OwlDemo/Catalog Menu/Models & UI/CatalogItemCell.swift @@ -22,7 +22,7 @@ public class CatalogItemCell: UITableViewCell { } } - public static var reusableViewSource: ReusableViewSource { + public class var reusableViewSource: ReusableViewSource { return .fromClass } diff --git a/OwlDemo/Contacts/Models & UI/CellUser.swift b/OwlDemo/Contacts/Models & UI/CellUser.swift index 441fa2e..0a7b310 100644 --- a/OwlDemo/Contacts/Models & UI/CellUser.swift +++ b/OwlDemo/Contacts/Models & UI/CellUser.swift @@ -11,4 +11,9 @@ import UIKit public class CellUser: UITableViewCell { @IBOutlet public var nameSurnameLabel: UILabel? @IBOutlet public var biographyLabel: UILabel? + + public static var reusableViewSource: ReusableViewSource { + return .fromStoryboard + } + } diff --git a/OwlDemo/Contacts/Models & UI/GroupFooterView.swift b/OwlDemo/Contacts/Models & UI/GroupFooterView.swift index 8a1c399..8074f61 100644 --- a/OwlDemo/Contacts/Models & UI/GroupFooterView.swift +++ b/OwlDemo/Contacts/Models & UI/GroupFooterView.swift @@ -10,4 +10,10 @@ import UIKit public class GroupFooterView: UITableViewHeaderFooterView { @IBOutlet public var footerLabel: UILabel? + + public class var reusableViewSource: ReusableViewSource { + return .fromClass + } + + } diff --git a/OwlDemo/Contacts/Models & UI/GroupHeaderView.swift b/OwlDemo/Contacts/Models & UI/GroupHeaderView.swift index 3d17184..3045ea3 100644 --- a/OwlDemo/Contacts/Models & UI/GroupHeaderView.swift +++ b/OwlDemo/Contacts/Models & UI/GroupHeaderView.swift @@ -11,5 +11,4 @@ import UIKit public class GroupHeaderView: UITableViewHeaderFooterView { @IBOutlet public var headerTitleLabel: UILabel? @IBOutlet public var headerSubtitleLabel: UILabel? - } diff --git a/OwlDemo/Emoji/UI/EmojiHeaderView.swift b/OwlDemo/Emoji/UI/EmojiHeaderView.swift index 934b2d8..d9d99a8 100644 --- a/OwlDemo/Emoji/UI/EmojiHeaderView.swift +++ b/OwlDemo/Emoji/UI/EmojiHeaderView.swift @@ -11,5 +11,5 @@ import UIKit public class EmojiHeaderView: UICollectionReusableView { @IBOutlet public var titleLabel: UILabel! - + } diff --git a/Sources/Collection/Cell Adapter/CollectionCellAdapter.swift b/Sources/Collection/Cell Adapter/CollectionCellAdapter.swift index 28ce8cd..b638ba4 100644 --- a/Sources/Collection/Cell Adapter/CollectionCellAdapter.swift +++ b/Sources/Collection/Cell Adapter/CollectionCellAdapter.swift @@ -30,11 +30,11 @@ public class CollectionCellAdapter UICollectionViewCell { - return collection.dequeueReusableCell(withReuseIdentifier: Cell.reusableViewIdentifier, for: indexPath) + return collection.dequeueReusableCell(withReuseIdentifier: Cell.reusableViewIdentifier(), for: indexPath) } public func registerReusableCellViewForDirector(_ director: CollectionDirector) -> Bool { - let id = Cell.reusableViewIdentifier + let id = Cell.reusableViewIdentifier() guard director.cellReuseIDs.contains(id) == false else { return false } diff --git a/Sources/Collection/HeaderFooter Adapter/CollectionHeaderFooterAdapter.swift b/Sources/Collection/HeaderFooter Adapter/CollectionHeaderFooterAdapter.swift index 2961fb2..c2728c1 100644 --- a/Sources/Collection/HeaderFooter Adapter/CollectionHeaderFooterAdapter.swift +++ b/Sources/Collection/HeaderFooter Adapter/CollectionHeaderFooterAdapter.swift @@ -43,19 +43,19 @@ public class CollectionHeaderFooterAdapter: Coll } public func dequeueHeaderFooterForDirector(_ director: CollectionDirector, type: String, indexPath: IndexPath) -> UICollectionReusableView? { - let identifier = View.reusableViewIdentifier + let identifier = View.reusableViewIdentifier() return director.collection?.dequeueReusableSupplementaryView(ofKind: type, withReuseIdentifier: identifier, for: indexPath) } public func registerHeaderFooterViewForDirector(_ director: CollectionDirector, kind: String) -> String { - let identifier = View.reusableViewIdentifier + let identifier = View.reusableViewIdentifier() if (kind == UICollectionView.elementKindSectionHeader && director.headerReuseIDs.contains(identifier)) || (kind == UICollectionView.elementKindSectionFooter && director.footerReuseIDs.contains(identifier)) { return identifier } let collection = director.collection - switch View.reusableViewSource { + switch View.reusableViewSource() { case .fromStoryboard: break @@ -64,7 +64,7 @@ public class CollectionHeaderFooterAdapter: Coll collection?.register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier) case .fromClass: - collection?.register(View.reusableViewClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier) + collection?.register(View.reusableViewClass(), forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier) } diff --git a/Sources/Shared/Protocols/ReusableCellViewProtocol.swift b/Sources/Shared/Protocols/ReusableCellViewProtocol.swift index 929be9d..d005a7c 100644 --- a/Sources/Shared/Protocols/ReusableCellViewProtocol.swift +++ b/Sources/Shared/Protocols/ReusableCellViewProtocol.swift @@ -14,13 +14,13 @@ import UIKit public protocol ReusableCellViewProtocol: class { - static var reusableViewType: AnyObject.Type { get } - static var reusableViewClass: AnyClass { get } - static var reusableViewIdentifier: String { get } - static var reusableViewSource: ReusableViewSource { get } + static func reusableViewType() -> AnyObject.Type + static func reusableViewClass() -> AnyClass + static func reusableViewIdentifier() -> String + static func reusableViewSource() -> ReusableViewSource static func registerReusableView(inTable table: UITableView?, as type: ReusableViewRegistrationType) - static func registerReusableView(inCollection collection: UICollectionView?, as type: ReusableViewRegistrationType) + static func registerReusableView(inCollection collection: UICollectionView?, as type: ReusableViewRegistrationType) } public enum ReusableViewSource { @@ -30,21 +30,29 @@ public enum ReusableViewSource { } public extension ReusableCellViewProtocol { + + static func reusableViewClass() -> AnyClass { + return self + } - static var reusableViewType: AnyObject.Type { - return reusableViewClass.self + static func reusableViewType() -> AnyObject.Type { + return reusableViewClass().self } - static var reusableViewSource: ReusableViewSource { + static func reusableViewSource() -> ReusableViewSource { return .fromStoryboard } - static var reusableViewIdentifier: String { + static func reusableViewIdentifier() -> String { return String(describing: reusableViewType) } - static func registerReusableView(inTable table: UITableView?, as type: ReusableViewRegistrationType) { - switch reusableViewSource { + static func registerReusableView(inTable table: UITableView?, as type: ReusableViewRegistrationType) { + + let reusableID = reusableViewIdentifier() + let reusableClass: AnyClass = reusableViewClass() + + switch reusableViewSource() { case .fromStoryboard: if type.isHeaderFooter { fatalError("Cannot load header/footer from storyboard. Use another source (xib/class) instead.") @@ -52,27 +60,31 @@ public extension ReusableCellViewProtocol { break case .fromXib(let name, let bundle): - let srcBundle = (bundle ?? Bundle.init(for: reusableViewClass)) - let srcNib = UINib(nibName: (name ?? reusableViewIdentifier), bundle: srcBundle) + let srcBundle = (bundle ?? Bundle.init(for: reusableClass)) + let srcNib = UINib(nibName: (name ?? reusableID), bundle: srcBundle) if type == .cell { - table?.register(srcNib, forCellReuseIdentifier: reusableViewIdentifier) + table?.register(srcNib, forCellReuseIdentifier: reusableID) } else { - table?.register(srcNib, forHeaderFooterViewReuseIdentifier: reusableViewIdentifier) + table?.register(srcNib, forHeaderFooterViewReuseIdentifier: reusableID) } case .fromClass: if type == .cell { - table?.register(reusableViewClass, forCellReuseIdentifier: reusableViewIdentifier) + table?.register(reusableClass, forCellReuseIdentifier: reusableID) } else { - table?.register(reusableViewClass, forHeaderFooterViewReuseIdentifier: reusableViewIdentifier) + table?.register(reusableClass, forHeaderFooterViewReuseIdentifier: reusableID) } } } - static func registerReusableView(inCollection collection: UICollectionView?, as type: ReusableViewRegistrationType) { - switch reusableViewSource { + static func registerReusableView(inCollection collection: UICollectionView?, as type: ReusableViewRegistrationType) { + + let reusableID = reusableViewIdentifier() + let reusableClass: AnyClass = reusableViewClass() + + switch reusableViewSource() { case .fromStoryboard: if type.isHeaderFooter { fatalError("Cannot load header/footer from storyboard. Use another source (xib/class) instead.") @@ -80,26 +92,26 @@ public extension ReusableCellViewProtocol { break case .fromXib(let name, let bundle): - let srcBundle = (bundle ?? Bundle.init(for: reusableViewClass)) - let srcNib = UINib(nibName: (name ?? reusableViewIdentifier), bundle: srcBundle) + let srcBundle = (bundle ?? Bundle.init(for: reusableClass)) + let srcNib = UINib(nibName: (name ?? reusableID), bundle: srcBundle) switch type { case .cell: - collection?.register(srcNib, forCellWithReuseIdentifier: reusableViewIdentifier) + collection?.register(srcNib, forCellWithReuseIdentifier: reusableID) case .header: - collection?.register(srcNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reusableViewIdentifier) + collection?.register(srcNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reusableID) case .footer: - collection?.register(srcNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reusableViewIdentifier) + collection?.register(srcNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reusableID) } case .fromClass: switch type { case .cell: - collection?.register(reusableViewClass, forCellWithReuseIdentifier: reusableViewIdentifier) + collection?.register(reusableClass, forCellWithReuseIdentifier: reusableID) case .header: - collection?.register(reusableViewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reusableViewIdentifier) + collection?.register(reusableClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reusableID) case .footer: - collection?.register(reusableViewClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reusableViewIdentifier) + collection?.register(reusableClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reusableID) } } @@ -120,14 +132,16 @@ public enum ReusableViewRegistrationType { } } + extension UITableViewCell : ReusableCellViewProtocol { - public static var reusableViewClass: AnyClass { + public static func reusableViewClass() -> AnyClass { return self } } extension UICollectionReusableView: ReusableCellViewProtocol { - public static var reusableViewClass: AnyClass { + public static func reusableViewClass() -> AnyClass { return self } } + diff --git a/Sources/Table/Cell Adapters/TableCellAdapter.swift b/Sources/Table/Cell Adapters/TableCellAdapter.swift index ddc68d0..7bc5b53 100644 --- a/Sources/Table/Cell Adapters/TableCellAdapter.swift +++ b/Sources/Table/Cell Adapters/TableCellAdapter.swift @@ -34,15 +34,15 @@ open class TableCellAdapter UITableViewCell { guard let indexPath = indexPath else { - let castedCell = Cell.reusableViewClass as! UITableViewCell.Type + let castedCell = Cell.reusableViewClass() as! UITableViewCell.Type let cellInstance = castedCell.init() return cellInstance } - return table.dequeueReusableCell(withIdentifier: Cell.reusableViewIdentifier, for: indexPath) + return table.dequeueReusableCell(withIdentifier: Cell.reusableViewIdentifier(), for: indexPath) } public func registerReusableCellViewForDirector(_ director: TableDirector) -> Bool { - let id = Cell.reusableViewIdentifier + let id = Cell.reusableViewIdentifier() guard director.cellReuseIDs.contains(id) == false else { return false } diff --git a/Sources/Table/Header Adapter/TableHeaderFooterAdapter.swift b/Sources/Table/Header Adapter/TableHeaderFooterAdapter.swift index f4aa122..2d86015 100644 --- a/Sources/Table/Header Adapter/TableHeaderFooterAdapter.swift +++ b/Sources/Table/Header Adapter/TableHeaderFooterAdapter.swift @@ -52,7 +52,7 @@ public class TableHeaderFooterAdapter: TableH // MARK: - Helper Function s- public func registerHeaderFooterViewForDirector(_ director: TableDirector) -> String { - let id = View.reusableViewIdentifier + let id = View.reusableViewIdentifier() guard director.headerFooterReuseIdentifiers.contains(id) == false else { return id } @@ -61,7 +61,7 @@ public class TableHeaderFooterAdapter: TableH } public func dequeueHeaderFooterForDirector(_ director: TableDirector) -> UITableViewHeaderFooterView? { - let id = View.reusableViewIdentifier + let id = View.reusableViewIdentifier() return director.table?.dequeueReusableHeaderFooterView(withIdentifier: id) } diff --git a/Sources/Table/Protocols & Supports/TableSectionHeaderFooterProtocol.swift b/Sources/Table/Protocols & Supports/TableSectionHeaderFooterProtocol.swift index 9360618..1ba0381 100644 --- a/Sources/Table/Protocols & Supports/TableSectionHeaderFooterProtocol.swift +++ b/Sources/Table/Protocols & Supports/TableSectionHeaderFooterProtocol.swift @@ -26,8 +26,8 @@ extension UITableViewHeaderFooterView: ReusableCellViewProtocol { return self } - public static var reusableViewSource: ReusableViewSource { + /*public static var reusableViewSource: ReusableViewSource { return .fromXib(name: nil,bundle: nil) - } + }*/ } diff --git a/Sources/Table/TableDirector.swift b/Sources/Table/TableDirector.swift index 91f0479..3bdb767 100644 --- a/Sources/Table/TableDirector.swift +++ b/Sources/Table/TableDirector.swift @@ -186,7 +186,7 @@ open class TableDirector: NSObject { @usableFromInline internal func adapterForHeaderFooterView(_ view: UIView) -> TableHeaderFooterAdapterProtocol? { guard let view = view as? UITableViewHeaderFooterView else { return nil } - return headerFooterAdapters[type(of: view).reusableViewIdentifier] + return headerFooterAdapters[type(of: view).reusableViewIdentifier()] } // MARK: - Add Sections -