Skip to content

Commit

Permalink
introducing a new type CellBinder.RegistrationMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlprliu committed Sep 13, 2018
1 parent ef494bd commit a3008a9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
24 changes: 19 additions & 5 deletions DataSourceKit/CellBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@
import Foundation

public struct CellBinder {
public let nib: UINib?
public let cellType: AnyClass?

public enum RegistrationMethod {
case nib(UINib)
case `class`(AnyClass)
case none
}

public let registrationMethod: RegistrationMethod
public let reuseIdentifier: String

internal let configureCell: (Any) -> Void

public init<Cell>(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.nib = nib
self.cellType = cellType as? AnyClass
public init<Cell>(cellType: Cell.Type, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.init(cellType: cellType, registrationMethod: .class(cellType as! AnyClass), reuseIdentifier: reuseIdentifier, configureCell: configureCell)
}

public init<Cell>(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.registrationMethod = registrationMethod
self.reuseIdentifier = reuseIdentifier
self.configureCell = { cell in
guard let cell = cell as? Cell else {
Expand All @@ -27,4 +36,9 @@ public struct CellBinder {
configureCell(cell)
}
}

@available(*, deprecated, message: "Use `init<Cell>(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead")
public init<Cell>(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
fatalError("This method is deprecated, Use `init<Cell>(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead")
}
}
10 changes: 6 additions & 4 deletions DataSourceKit/CollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class CollectionViewDataSource<CellDeclaration>: NSObject, UICollectionVi
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item])
if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) {

if let nib = cellBinder.nib {
switch cellBinder.registrationMethod {
case let .nib(nib):
collectionView.register(nib, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
} else {
collectionView.register(cellBinder.cellType, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
case let .class(cellClass):
collectionView.register(cellClass, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
case .none:
break
}
registeredReuseIdentifiers.append(cellBinder.reuseIdentifier)
}
Expand Down
11 changes: 6 additions & 5 deletions DataSourceKit/TableViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ public class TableViewDataSource<CellDeclaration>: NSObject, UITableViewDataSour
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item])
if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) {
if let nib = cellBinder.nib {
switch cellBinder.registrationMethod {
case let .nib(nib):
tableView.register(nib, forCellReuseIdentifier: cellBinder.reuseIdentifier)
} else {
tableView.register(cellBinder.cellType, forCellReuseIdentifier: cellBinder.reuseIdentifier)
case let .class(cellClass):
tableView.register(cellClass, forCellReuseIdentifier: cellBinder.reuseIdentifier)
case .none:
break
}
registeredReuseIdentifiers.append(cellBinder.reuseIdentifier)
}

let cell = tableView.dequeueReusableCell(withIdentifier: cellBinder.reuseIdentifier, for: indexPath)
cellBinder.configureCell(cell)

return cell
}
}
2 changes: 1 addition & 1 deletion Demo/Cell/RelatedVenueCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension RelatedVenueCell: BindableCell {
static func makeBinder(value venue: Venue) -> CellBinder {
return CellBinder(
cellType: RelatedVenueCell.self,
nib: UINib(nibName: "RelatedVenueCell", bundle: nil),
registrationMethod: .nib(UINib(nibName: "RelatedVenueCell", bundle: nil)),
reuseIdentifier: "RelatedVenue",
configureCell: { cell in
cell.photoImageView.image = venue.photo
Expand Down
2 changes: 1 addition & 1 deletion Demo/Cell/ReviewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension ReviewCell: BindableCell {
static func makeBinder(value review: Review) -> CellBinder {
return CellBinder(
cellType: ReviewCell.self,
nib: UINib(nibName: "ReviewCell", bundle: nil),
registrationMethod: .nib(UINib(nibName: "ReviewCell", bundle: nil)),
reuseIdentifier: "Review",
configureCell: { cell in
cell.authorImageView.image = review.authorImage
Expand Down
2 changes: 1 addition & 1 deletion Demo/Cell/SectionHeaderCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension SectionHeaderCell: BindableCell {
static func makeBinder(value title: String) -> CellBinder {
return CellBinder(
cellType: SectionHeaderCell.self,
nib: UINib(nibName: "SectionHeaderCell", bundle: nil),
registrationMethod: .nib(UINib(nibName: "SectionHeaderCell", bundle: nil)),
reuseIdentifier: "SectionHeader",
configureCell: { cell in
cell.titleLabel.text = title
Expand Down
2 changes: 1 addition & 1 deletion Demo/Cell/VenueOutlineCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension VenueOutlineCell: BindableCell {
static func makeBinder(value venue: Venue) -> CellBinder {
return CellBinder(
cellType: VenueOutlineCell.self,
nib: UINib(nibName: "VenueOutlineCell", bundle: nil),
registrationMethod: .nib(UINib(nibName: "VenueOutlineCell", bundle: nil)),
reuseIdentifier: "VenueOutline",
configureCell: { cell in
cell.photoImageView.image = venue.photo
Expand Down

0 comments on commit a3008a9

Please sign in to comment.