Skip to content

Commit

Permalink
#14 Fix for ReusableCellViewProtocol override
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed May 23, 2019
1 parent 17c624f commit abfdc44
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion OwlDemo/Catalog Menu/Models & UI/CatalogItemCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CatalogItemCell: UITableViewCell {
}
}

public static var reusableViewSource: ReusableViewSource {
public class var reusableViewSource: ReusableViewSource {
return .fromClass
}

Expand Down
5 changes: 5 additions & 0 deletions OwlDemo/Contacts/Models & UI/CellUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
6 changes: 6 additions & 0 deletions OwlDemo/Contacts/Models & UI/GroupFooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ import UIKit

public class GroupFooterView: UITableViewHeaderFooterView {
@IBOutlet public var footerLabel: UILabel?

public class var reusableViewSource: ReusableViewSource {
return .fromClass
}


}
1 change: 0 additions & 1 deletion OwlDemo/Contacts/Models & UI/GroupHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ import UIKit
public class GroupHeaderView: UITableViewHeaderFooterView {
@IBOutlet public var headerTitleLabel: UILabel?
@IBOutlet public var headerSubtitleLabel: UILabel?

}
2 changes: 1 addition & 1 deletion OwlDemo/Emoji/UI/EmojiHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import UIKit
public class EmojiHeaderView: UICollectionReusableView {

@IBOutlet public var titleLabel: UILabel!

}
4 changes: 2 additions & 2 deletions Sources/Collection/Cell Adapter/CollectionCellAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class CollectionCellAdapter<Model: ElementRepresentable, Cell: ReusableCe
// MARK: - Adapter Helpers Functions -

public func dequeueCell(inCollection collection: UICollectionView, at indexPath: IndexPath) -> 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public class CollectionHeaderFooterAdapter<View: UICollectionReusableView>: 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

Expand All @@ -64,7 +64,7 @@ public class CollectionHeaderFooterAdapter<View: UICollectionReusableView>: 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)

}

Expand Down
72 changes: 43 additions & 29 deletions Sources/Shared/Protocols/ReusableCellViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,76 +30,88 @@ 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.")
}
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.")
}
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)
}

}
Expand All @@ -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
}
}

6 changes: 3 additions & 3 deletions Sources/Table/Cell Adapters/TableCellAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ open class TableCellAdapter<Model: ElementRepresentable, Cell: ReusableCellViewP

public func dequeueCell(inTable table: UITableView, at indexPath: IndexPath?) -> 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
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Table/Header Adapter/TableHeaderFooterAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class TableHeaderFooterAdapter<View: UITableViewHeaderFooterView>: 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
}
Expand All @@ -61,7 +61,7 @@ public class TableHeaderFooterAdapter<View: UITableViewHeaderFooterView>: TableH
}

public func dequeueHeaderFooterForDirector(_ director: TableDirector) -> UITableViewHeaderFooterView? {
let id = View.reusableViewIdentifier
let id = View.reusableViewIdentifier()
return director.table?.dequeueReusableHeaderFooterView(withIdentifier: id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}*/

}
2 changes: 1 addition & 1 deletion Sources/Table/TableDirector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down

0 comments on commit abfdc44

Please sign in to comment.