From ef494bdc953e3d339a9e833f5e1adedd3b4677e2 Mon Sep 17 00:00:00 2001 From: dvlprliu Date: Mon, 10 Sep 2018 13:24:56 +0800 Subject: [PATCH 1/5] change: make CellBinder's nib property optional --- DataSourceKit.xcodeproj/project.pbxproj | 12 ++++++ .../contents.xcworkspacedata | 7 ++++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ DataSourceKit/CellBinder.swift | 6 ++- DataSourceKit/CollectionViewDataSource.swift | 7 +++- DataSourceKit/TableViewDataSource.swift | 6 ++- .../CollectionViewDataSourceTests.swift | 17 +++++++- DataSourceKitTests/Example/C.swift | 13 ++++++ .../Example/CCollectionViewCell.swift | 26 ++++++++++++ .../Example/CTableViewCell.swift | 40 +++++++++++++++++++ .../TableViewDataSourceTests.swift | 17 +++++++- .../TestView/TestCollectionView.swift | 17 ++++++++ .../TestView/TestTableView.swift | 13 ++++++ 13 files changed, 181 insertions(+), 8 deletions(-) create mode 100644 DataSourceKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 DataSourceKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 DataSourceKitTests/Example/C.swift create mode 100644 DataSourceKitTests/Example/CCollectionViewCell.swift create mode 100644 DataSourceKitTests/Example/CTableViewCell.swift diff --git a/DataSourceKit.xcodeproj/project.pbxproj b/DataSourceKit.xcodeproj/project.pbxproj index ffd8818..be05d91 100644 --- a/DataSourceKit.xcodeproj/project.pbxproj +++ b/DataSourceKit.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 553ACE5D21461F1700710FDF /* C.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE5C21461F1700710FDF /* C.swift */; }; + 553ACE5F21461F4B00710FDF /* CCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */; }; + 553ACE6121461F7300710FDF /* CTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE6021461F7300710FDF /* CTableViewCell.swift */; }; 7F59442B213FA22E007300FC /* TableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59442A213FA22E007300FC /* TableViewDataSource.swift */; }; 7F9AEDAD213BBBA10092854F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F9AEDAC213BBBA10092854F /* AppDelegate.swift */; }; 7F9AEDB4213BBBA30092854F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7F9AEDB3213BBBA30092854F /* Assets.xcassets */; }; @@ -89,6 +92,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 553ACE5C21461F1700710FDF /* C.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C.swift; sourceTree = ""; }; + 553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CCollectionViewCell.swift; sourceTree = ""; }; + 553ACE6021461F7300710FDF /* CTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CTableViewCell.swift; sourceTree = ""; }; 7F59442A213FA22E007300FC /* TableViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewDataSource.swift; sourceTree = ""; }; 7F9AEDAA213BBBA10092854F /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7F9AEDAC213BBBA10092854F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -290,6 +296,9 @@ 7FB64C1E213B5BC80071FC26 /* BCollectionViewCell.swift */, 7FC38D2E213FA8B3009FCEB1 /* BTableViewCell.xib */, 7FC38D30213FA8BC009FCEB1 /* BTableViewCell.swift */, + 553ACE5C21461F1700710FDF /* C.swift */, + 553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */, + 553ACE6021461F7300710FDF /* CTableViewCell.swift */, ); path = Example; sourceTree = ""; @@ -530,8 +539,11 @@ 7FC38D29213FA6B7009FCEB1 /* B.swift in Sources */, 7FC38D33213FA908009FCEB1 /* TableViewDataSourceTests.swift in Sources */, 7FB64C1F213B5BC80071FC26 /* BCollectionViewCell.swift in Sources */, + 553ACE5D21461F1700710FDF /* C.swift in Sources */, 7FB64C1D213B5BC20071FC26 /* ACollectionViewCell.swift in Sources */, 7FB64C1B213B59300071FC26 /* TestCollectionView.swift in Sources */, + 553ACE6121461F7300710FDF /* CTableViewCell.swift in Sources */, + 553ACE5F21461F4B00710FDF /* CCollectionViewCell.swift in Sources */, 7FC38D35213FA917009FCEB1 /* TestTableView.swift in Sources */, 7FC38D31213FA8BC009FCEB1 /* BTableViewCell.swift in Sources */, 7FC38D27213FA585009FCEB1 /* A.swift in Sources */, diff --git a/DataSourceKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/DataSourceKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/DataSourceKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/DataSourceKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/DataSourceKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/DataSourceKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/DataSourceKit/CellBinder.swift b/DataSourceKit/CellBinder.swift index 5bce812..ed7bf87 100644 --- a/DataSourceKit/CellBinder.swift +++ b/DataSourceKit/CellBinder.swift @@ -9,13 +9,15 @@ import Foundation public struct CellBinder { - public let nib: UINib + public let nib: UINib? + public let cellType: AnyClass? public let reuseIdentifier: String internal let configureCell: (Any) -> Void - public init(cellType: Cell.Type, nib: UINib, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { + public init(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { self.nib = nib + self.cellType = cellType as? AnyClass self.reuseIdentifier = reuseIdentifier self.configureCell = { cell in guard let cell = cell as? Cell else { diff --git a/DataSourceKit/CollectionViewDataSource.swift b/DataSourceKit/CollectionViewDataSource.swift index 32f820c..1ec23d4 100644 --- a/DataSourceKit/CollectionViewDataSource.swift +++ b/DataSourceKit/CollectionViewDataSource.swift @@ -26,7 +26,12 @@ public class CollectionViewDataSource: NSObject, UICollectionVi public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item]) if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) { - collectionView.register(cellBinder.nib, forCellWithReuseIdentifier: cellBinder.reuseIdentifier) + + if let nib = cellBinder.nib { + collectionView.register(nib, forCellWithReuseIdentifier: cellBinder.reuseIdentifier) + } else { + collectionView.register(cellBinder.cellType, forCellWithReuseIdentifier: cellBinder.reuseIdentifier) + } registeredReuseIdentifiers.append(cellBinder.reuseIdentifier) } diff --git a/DataSourceKit/TableViewDataSource.swift b/DataSourceKit/TableViewDataSource.swift index 2c94d21..e6960e8 100644 --- a/DataSourceKit/TableViewDataSource.swift +++ b/DataSourceKit/TableViewDataSource.swift @@ -26,7 +26,11 @@ public class TableViewDataSource: NSObject, UITableViewDataSour public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item]) if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) { - tableView.register(cellBinder.nib, forCellReuseIdentifier: cellBinder.reuseIdentifier) + if let nib = cellBinder.nib { + tableView.register(nib, forCellReuseIdentifier: cellBinder.reuseIdentifier) + } else { + tableView.register(cellBinder.cellType, forCellReuseIdentifier: cellBinder.reuseIdentifier) + } registeredReuseIdentifiers.append(cellBinder.reuseIdentifier) } diff --git a/DataSourceKitTests/CollectionViewDataSourceTests.swift b/DataSourceKitTests/CollectionViewDataSourceTests.swift index c2dc2c1..7b2fc0c 100644 --- a/DataSourceKitTests/CollectionViewDataSourceTests.swift +++ b/DataSourceKitTests/CollectionViewDataSourceTests.swift @@ -14,11 +14,13 @@ class CollectionViewDataSourceTests: XCTestCase { enum CellDeclaration { case a(A) case b(B) + case c(C) } struct Data: CellsDeclarator { var a: [A] var b: [B] + var c: [C] func declareCells(_ cell: (CellDeclaration) -> Void) { for a in a { @@ -27,6 +29,9 @@ class CollectionViewDataSourceTests: XCTestCase { for b in b { cell(.b(b)) } + for c in c { + cell(.c(c)) + } } } @@ -42,6 +47,8 @@ class CollectionViewDataSourceTests: XCTestCase { return ACollectionViewCell.makeBinder(value: a) case .b(let b): return BCollectionViewCell.makeBinder(value: b) + case .c(let c): + return CCollectionViewCell.makeBinder(value: c) } } @@ -50,13 +57,14 @@ class CollectionViewDataSourceTests: XCTestCase { } func test() { - let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)]) + let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)], c: [C(id: 1)]) dataSource.cellDeclarations = data.cellDeclarations collectionView.reloadData() collectionView.layoutIfNeeded() - XCTAssertEqual(dataSource.collectionView(collectionView, numberOfItemsInSection: 0), 3) + XCTAssertEqual(dataSource.collectionView(collectionView, numberOfItemsInSection: 0), 4) XCTAssertEqual(collectionView.nibRegistrations.map({ $0.reuseIdentifier }), ["ACollectionViewCell", "BCollectionViewCell"]) + XCTAssertEqual(collectionView.classRegistrations.map({ $0.reuseIdentifier }), ["CCollectionViewCell"]) let verifiers: [(UICollectionViewCell?) -> Void] = [ { cell in @@ -74,6 +82,11 @@ class CollectionViewDataSourceTests: XCTestCase { XCTAssertNotNil(cell) XCTAssertEqual(cell?.idLabel.text, "1") }, + { cell in + let cell = cell as? CCollectionViewCell + XCTAssertNotNil(cell) + XCTAssertEqual(cell?.idLabel.text, "1") + } ] for (index, verifier) in verifiers.enumerated() { diff --git a/DataSourceKitTests/Example/C.swift b/DataSourceKitTests/Example/C.swift new file mode 100644 index 0000000..52d071b --- /dev/null +++ b/DataSourceKitTests/Example/C.swift @@ -0,0 +1,13 @@ +// +// C.swift +// DataSourceKitTests +// +// Created by zhzh liu on 2018/9/10. +// Copyright © 2018年 Yosuke Ishikawa. All rights reserved. +// + +import Foundation + +struct C { + let id: Int64 +} diff --git a/DataSourceKitTests/Example/CCollectionViewCell.swift b/DataSourceKitTests/Example/CCollectionViewCell.swift new file mode 100644 index 0000000..e1bf02b --- /dev/null +++ b/DataSourceKitTests/Example/CCollectionViewCell.swift @@ -0,0 +1,26 @@ +// +// CCollectionViewCell.swift +// DataSourceKitTests +// +// Created by zhzh liu on 2018/9/10. +// Copyright © 2018年 Yosuke Ishikawa. All rights reserved. +// + +import UIKit +import DataSourceKit + +class CCollectionViewCell: UICollectionViewCell { + var idLabel = UILabel(frame: .zero) +} + +extension CCollectionViewCell: BindableCell { + static func makeBinder(value: C) -> CellBinder { + return CellBinder( + cellType: CCollectionViewCell.self, + nib: nil, + reuseIdentifier: "CCollectionViewCell", + configureCell: { (cell) in + cell.idLabel.text = "\(value.id)" + }) + } +} diff --git a/DataSourceKitTests/Example/CTableViewCell.swift b/DataSourceKitTests/Example/CTableViewCell.swift new file mode 100644 index 0000000..36a87c7 --- /dev/null +++ b/DataSourceKitTests/Example/CTableViewCell.swift @@ -0,0 +1,40 @@ +// +// CTableViewCell.swift +// DataSourceKitTests +// +// Created by zhzh liu on 2018/9/10. +// Copyright © 2018年 Yosuke Ishikawa. All rights reserved. +// + +import UIKit +import DataSourceKit + +class CTableViewCell: UITableViewCell { + var idLabel = UILabel(frame: .zero) + + override init(style: UITableViewCellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + contentView.addSubview(idLabel) + idLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor) + idLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor) + idLabel.heightAnchor.constraint(equalToConstant: 44.0) + idLabel.topAnchor.constraint(equalTo: contentView.topAnchor) + idLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) + } + + required init?(coder aDecoder: NSCoder) { + fatalError() + } +} + +extension CTableViewCell: BindableCell { + static func makeBinder(value: C) -> CellBinder { + return CellBinder( + cellType: CTableViewCell.self, + nib: nil, + reuseIdentifier: "CTableViewCell", + configureCell: { (cell) in + cell.idLabel.text = "\(value.id)" + }) + } +} diff --git a/DataSourceKitTests/TableViewDataSourceTests.swift b/DataSourceKitTests/TableViewDataSourceTests.swift index e1cfc76..758b116 100644 --- a/DataSourceKitTests/TableViewDataSourceTests.swift +++ b/DataSourceKitTests/TableViewDataSourceTests.swift @@ -14,11 +14,13 @@ class TableViewDataSourceTests: XCTestCase { enum CellDeclaration { case a(A) case b(B) + case c(C) } struct Data: CellsDeclarator { var a: [A] var b: [B] + var c: [C] func declareCells(_ cell: (CellDeclaration) -> Void) { for a in a { @@ -27,6 +29,9 @@ class TableViewDataSourceTests: XCTestCase { for b in b { cell(.b(b)) } + for c in c { + cell(.c(c)) + } } } @@ -42,6 +47,8 @@ class TableViewDataSourceTests: XCTestCase { return ATableViewCell.makeBinder(value: a) case .b(let b): return BTableViewCell.makeBinder(value: b) + case .c(let c): + return CTableViewCell.makeBinder(value: c) } } @@ -50,13 +57,14 @@ class TableViewDataSourceTests: XCTestCase { } func test() { - let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)]) + let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)], c: [C(id: 1)]) dataSource.cellDeclarations = data.cellDeclarations tableView.reloadData() tableView.layoutIfNeeded() - XCTAssertEqual(dataSource.tableView(tableView, numberOfRowsInSection: 0), 3) + XCTAssertEqual(dataSource.tableView(tableView, numberOfRowsInSection: 0), 4) XCTAssertEqual(tableView.nibRegistrations.map({ $0.reuseIdentifier }), ["ATableViewCell", "BTableViewCell"]) + XCTAssertEqual(tableView.classRegistrations.map({ $0.reuseIdentifier }), ["CTableViewCell"]) let verifiers: [(UITableViewCell?) -> Void] = [ { cell in @@ -74,6 +82,11 @@ class TableViewDataSourceTests: XCTestCase { XCTAssertNotNil(cell) XCTAssertEqual(cell?.idLabel.text, "1") }, + { cell in + let cell = cell as? CTableViewCell + XCTAssertNotNil(cell) + XCTAssertEqual(cell?.idLabel.text, "1") + } ] for (index, verifier) in verifiers.enumerated() { diff --git a/DataSourceKitTests/TestView/TestCollectionView.swift b/DataSourceKitTests/TestView/TestCollectionView.swift index e13202f..1ca7eaf 100644 --- a/DataSourceKitTests/TestView/TestCollectionView.swift +++ b/DataSourceKitTests/TestView/TestCollectionView.swift @@ -14,7 +14,13 @@ class TestCollectionView: UICollectionView { let nib: UINib? } + struct ClassRegistration { + let reuseIdentifier: String + let cellType: AnyClass? + } + var nibRegistrations = [] as [NibRegistration] + var classRegistrations = [] as [ClassRegistration] override func register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String) { super.register(nib, forCellWithReuseIdentifier: identifier) @@ -22,4 +28,15 @@ class TestCollectionView: UICollectionView { let nibRegistration = NibRegistration(reuseIdentifier: identifier, nib: nib) nibRegistrations.append(nibRegistration) } + + override func register(_ cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String) { + super.register(cellClass, forCellWithReuseIdentifier: identifier) + + // The `register(_ cellClass: forCellWithReuseIdentifier:)` method will be called and receiving a `"com.apple.UIKit.shadowReuseCellIdentifier"` + // reuse identifier when UICollectionView is initiated by `init(frame: collectionViewLayout:)`, I filter it out since it's useless for + // our testing. + if identifier == "com.apple.UIKit.shadowReuseCellIdentifier" { return } + let classRegistration = ClassRegistration(reuseIdentifier: identifier, cellType: cellClass) + classRegistrations.append(classRegistration) + } } diff --git a/DataSourceKitTests/TestView/TestTableView.swift b/DataSourceKitTests/TestView/TestTableView.swift index b39f38a..2bbd0d7 100644 --- a/DataSourceKitTests/TestView/TestTableView.swift +++ b/DataSourceKitTests/TestView/TestTableView.swift @@ -14,7 +14,13 @@ class TestTableView: UITableView { let nib: UINib? } + struct ClassRegistration { + let reuseIdentifier: String + let cellClass: AnyClass? + } + var nibRegistrations = [] as [NibRegistration] + var classRegistrations = [] as [ClassRegistration] override func register(_ nib: UINib?, forCellReuseIdentifier identifier: String) { super.register(nib, forCellReuseIdentifier: identifier) @@ -22,4 +28,11 @@ class TestTableView: UITableView { let nibRegistration = NibRegistration(reuseIdentifier: identifier, nib: nib) nibRegistrations.append(nibRegistration) } + + override func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String) { + super.register(cellClass, forCellReuseIdentifier: identifier) + + let classRegistration = ClassRegistration(reuseIdentifier: identifier, cellClass: cellClass) + classRegistrations.append(classRegistration) + } } From a3008a91740ac8aac93280d244af50e3d01fa210 Mon Sep 17 00:00:00 2001 From: dvlprliu Date: Thu, 13 Sep 2018 14:35:38 +0800 Subject: [PATCH 2/5] introducing a new type `CellBinder.RegistrationMethod` --- DataSourceKit/CellBinder.swift | 24 ++++++++++++++++---- DataSourceKit/CollectionViewDataSource.swift | 10 ++++---- DataSourceKit/TableViewDataSource.swift | 11 +++++---- Demo/Cell/RelatedVenueCell.swift | 2 +- Demo/Cell/ReviewCell.swift | 2 +- Demo/Cell/SectionHeaderCell.swift | 2 +- Demo/Cell/VenueOutlineCell.swift | 2 +- 7 files changed, 35 insertions(+), 18 deletions(-) diff --git a/DataSourceKit/CellBinder.swift b/DataSourceKit/CellBinder.swift index ed7bf87..5a30fad 100644 --- a/DataSourceKit/CellBinder.swift +++ b/DataSourceKit/CellBinder.swift @@ -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(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { - self.nib = nib - self.cellType = cellType as? AnyClass + public init(cellType: Cell.Type, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { + self.init(cellType: cellType, registrationMethod: .class(cellType as! AnyClass), reuseIdentifier: reuseIdentifier, configureCell: configureCell) + } + + public init(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 { @@ -27,4 +36,9 @@ public struct CellBinder { configureCell(cell) } } + + @available(*, deprecated, message: "Use `init(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead") + public init(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { + fatalError("This method is deprecated, Use `init(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead") + } } diff --git a/DataSourceKit/CollectionViewDataSource.swift b/DataSourceKit/CollectionViewDataSource.swift index 1ec23d4..3f20fe4 100644 --- a/DataSourceKit/CollectionViewDataSource.swift +++ b/DataSourceKit/CollectionViewDataSource.swift @@ -26,11 +26,13 @@ public class CollectionViewDataSource: 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) } diff --git a/DataSourceKit/TableViewDataSource.swift b/DataSourceKit/TableViewDataSource.swift index e6960e8..0d203d2 100644 --- a/DataSourceKit/TableViewDataSource.swift +++ b/DataSourceKit/TableViewDataSource.swift @@ -26,17 +26,18 @@ public class TableViewDataSource: 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 } } diff --git a/Demo/Cell/RelatedVenueCell.swift b/Demo/Cell/RelatedVenueCell.swift index 2aef2df..424d1c5 100644 --- a/Demo/Cell/RelatedVenueCell.swift +++ b/Demo/Cell/RelatedVenueCell.swift @@ -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 diff --git a/Demo/Cell/ReviewCell.swift b/Demo/Cell/ReviewCell.swift index 375600a..3aae4dc 100644 --- a/Demo/Cell/ReviewCell.swift +++ b/Demo/Cell/ReviewCell.swift @@ -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 diff --git a/Demo/Cell/SectionHeaderCell.swift b/Demo/Cell/SectionHeaderCell.swift index 332762a..7ae240f 100644 --- a/Demo/Cell/SectionHeaderCell.swift +++ b/Demo/Cell/SectionHeaderCell.swift @@ -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 diff --git a/Demo/Cell/VenueOutlineCell.swift b/Demo/Cell/VenueOutlineCell.swift index 047572c..cc768cf 100644 --- a/Demo/Cell/VenueOutlineCell.swift +++ b/Demo/Cell/VenueOutlineCell.swift @@ -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 From 907e11eb83bc5d1a1ef0c724eb1d110a0005f97a Mon Sep 17 00:00:00 2001 From: dvlprliu Date: Fri, 14 Sep 2018 11:04:27 +0800 Subject: [PATCH 3/5] udpate: delete convenience intitial method --- DataSourceKit/CellBinder.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/DataSourceKit/CellBinder.swift b/DataSourceKit/CellBinder.swift index 5a30fad..e366183 100644 --- a/DataSourceKit/CellBinder.swift +++ b/DataSourceKit/CellBinder.swift @@ -21,10 +21,6 @@ public struct CellBinder { internal let configureCell: (Any) -> Void - public init(cellType: Cell.Type, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { - self.init(cellType: cellType, registrationMethod: .class(cellType as! AnyClass), reuseIdentifier: reuseIdentifier, configureCell: configureCell) - } - public init(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { self.registrationMethod = registrationMethod self.reuseIdentifier = reuseIdentifier From 619d8951fa47c7ef94480737ea4577dfe012fa1d Mon Sep 17 00:00:00 2001 From: Yosuke Ishikawa Date: Fri, 14 Sep 2018 13:09:05 +0900 Subject: [PATCH 4/5] Redirect deprecated initializer to the new initialzier --- DataSourceKit/CellBinder.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataSourceKit/CellBinder.swift b/DataSourceKit/CellBinder.swift index e366183..2c8f1b8 100644 --- a/DataSourceKit/CellBinder.swift +++ b/DataSourceKit/CellBinder.swift @@ -34,7 +34,7 @@ public struct CellBinder { } @available(*, deprecated, message: "Use `init(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead") - public init(cellType: Cell.Type, nib: UINib?, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { - fatalError("This method is deprecated, Use `init(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead") + public init(cellType: Cell.Type, nib: UINib, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) { + self.init(cellType: cellType, registrationMethod: .nib(nib), reuseIdentifier: reuseIdentifier, configureCell: configureCell) } } From b4c8e149ec32619be89b39e8480140cd8594190e Mon Sep 17 00:00:00 2001 From: Yosuke Ishikawa Date: Fri, 14 Sep 2018 13:10:21 +0900 Subject: [PATCH 5/5] Use new initializer of CellBinder in tests --- DataSourceKitTests/Example/ACollectionViewCell.swift | 2 +- DataSourceKitTests/Example/ATableViewCell.swift | 2 +- DataSourceKitTests/Example/BCollectionViewCell.swift | 2 +- DataSourceKitTests/Example/BTableViewCell.swift | 2 +- DataSourceKitTests/Example/CCollectionViewCell.swift | 2 +- DataSourceKitTests/Example/CTableViewCell.swift | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DataSourceKitTests/Example/ACollectionViewCell.swift b/DataSourceKitTests/Example/ACollectionViewCell.swift index 98f5984..c7ac11f 100644 --- a/DataSourceKitTests/Example/ACollectionViewCell.swift +++ b/DataSourceKitTests/Example/ACollectionViewCell.swift @@ -17,7 +17,7 @@ extension ACollectionViewCell: BindableCell { static func makeBinder(value: A) -> CellBinder { return CellBinder( cellType: ACollectionViewCell.self, - nib: UINib(nibName: "ACollectionViewCell", bundle: Bundle(for: ACollectionViewCell.self)), + registrationMethod: .nib(UINib(nibName: "ACollectionViewCell", bundle: Bundle(for: ACollectionViewCell.self))), reuseIdentifier: "ACollectionViewCell", configureCell: { cell in cell.idLabel.text = String(value.id) diff --git a/DataSourceKitTests/Example/ATableViewCell.swift b/DataSourceKitTests/Example/ATableViewCell.swift index 7acf9bd..77266ab 100644 --- a/DataSourceKitTests/Example/ATableViewCell.swift +++ b/DataSourceKitTests/Example/ATableViewCell.swift @@ -17,7 +17,7 @@ extension ATableViewCell: BindableCell { static func makeBinder(value: A) -> CellBinder { return CellBinder( cellType: ATableViewCell.self, - nib: UINib(nibName: "ATableViewCell", bundle: Bundle(for: ATableViewCell.self)), + registrationMethod: .nib(UINib(nibName: "ATableViewCell", bundle: Bundle(for: ATableViewCell.self))), reuseIdentifier: "ATableViewCell", configureCell: { cell in cell.idLabel.text = String(value.id) diff --git a/DataSourceKitTests/Example/BCollectionViewCell.swift b/DataSourceKitTests/Example/BCollectionViewCell.swift index 361e164..f4e98bd 100644 --- a/DataSourceKitTests/Example/BCollectionViewCell.swift +++ b/DataSourceKitTests/Example/BCollectionViewCell.swift @@ -17,7 +17,7 @@ extension BCollectionViewCell: BindableCell { static func makeBinder(value: B) -> CellBinder { return CellBinder( cellType: BCollectionViewCell.self, - nib: UINib(nibName: "BCollectionViewCell", bundle: Bundle(for: BCollectionViewCell.self)), + registrationMethod: .nib(UINib(nibName: "BCollectionViewCell", bundle: Bundle(for: BCollectionViewCell.self))), reuseIdentifier: "BCollectionViewCell", configureCell: { cell in cell.idLabel.text = String(value.id) diff --git a/DataSourceKitTests/Example/BTableViewCell.swift b/DataSourceKitTests/Example/BTableViewCell.swift index 8c61df7..e84b1ad 100644 --- a/DataSourceKitTests/Example/BTableViewCell.swift +++ b/DataSourceKitTests/Example/BTableViewCell.swift @@ -17,7 +17,7 @@ extension BTableViewCell: BindableCell { static func makeBinder(value: B) -> CellBinder { return CellBinder( cellType: BTableViewCell.self, - nib: UINib(nibName: "BTableViewCell", bundle: Bundle(for: BTableViewCell.self)), + registrationMethod: .nib(UINib(nibName: "BTableViewCell", bundle: Bundle(for: BTableViewCell.self))), reuseIdentifier: "BTableViewCell", configureCell: { cell in cell.idLabel.text = String(value.id) diff --git a/DataSourceKitTests/Example/CCollectionViewCell.swift b/DataSourceKitTests/Example/CCollectionViewCell.swift index e1bf02b..937e3d9 100644 --- a/DataSourceKitTests/Example/CCollectionViewCell.swift +++ b/DataSourceKitTests/Example/CCollectionViewCell.swift @@ -17,7 +17,7 @@ extension CCollectionViewCell: BindableCell { static func makeBinder(value: C) -> CellBinder { return CellBinder( cellType: CCollectionViewCell.self, - nib: nil, + registrationMethod: .class(CCollectionViewCell.self), reuseIdentifier: "CCollectionViewCell", configureCell: { (cell) in cell.idLabel.text = "\(value.id)" diff --git a/DataSourceKitTests/Example/CTableViewCell.swift b/DataSourceKitTests/Example/CTableViewCell.swift index 36a87c7..7b39fd9 100644 --- a/DataSourceKitTests/Example/CTableViewCell.swift +++ b/DataSourceKitTests/Example/CTableViewCell.swift @@ -31,7 +31,7 @@ extension CTableViewCell: BindableCell { static func makeBinder(value: C) -> CellBinder { return CellBinder( cellType: CTableViewCell.self, - nib: nil, + registrationMethod: .class(CTableViewCell.self), reuseIdentifier: "CTableViewCell", configureCell: { (cell) in cell.idLabel.text = "\(value.id)"