Skip to content

Commit d91e4bf

Browse files
committed
Adapts collection and table view to use Swift 4.0 capabilities.
1 parent 0c4c437 commit d91e4bf

13 files changed

+328
-375
lines changed

Example/Example1_CustomizationUsingTableViewDelegate.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ class CustomizationUsingTableViewDelegate : UIViewController {
4242

4343
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
4444

45-
let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>()
45+
let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>(
46+
configureCell: { ds, tv, ip, item in
47+
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
48+
cell.textLabel?.text = "Item \(item)"
49+
50+
return cell
51+
},
52+
titleForHeaderInSection: { ds, index in
53+
return ds.sectionModels[index].header
54+
}
55+
)
4656

47-
dataSource.configureCell = { ds, tv, ip, item in
48-
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
49-
cell.textLabel?.text = "Item \(item)"
50-
51-
return cell
52-
}
53-
54-
dataSource.titleForHeaderInSection = { ds, index in
55-
return ds.sectionModels[index].header
56-
}
57+
self.dataSource = dataSource
5758

5859
let sections = [
5960
MySection(header: "First section", items: [
@@ -72,8 +73,6 @@ class CustomizationUsingTableViewDelegate : UIViewController {
7273

7374
tableView.rx.setDelegate(self)
7475
.addDisposableTo(disposeBag)
75-
76-
self.dataSource = dataSource
7776
}
7877
}
7978

@@ -88,6 +87,6 @@ extension CustomizationUsingTableViewDelegate : UITableViewDelegate {
8887
return 0.0
8988
}
9089

91-
return CGFloat(40 + item)
90+
return CGFloat(40 + item * 10)
9291
}
9392
}

Example/Example2_RandomizedSectionsAnimation.swift

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ class ViewController: UIViewController {
4444
return a.sections
4545
}
4646
.shareReplay(1)
47-
let tvAnimatedDataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>()
48-
let reloadDataSource = RxTableViewSectionedReloadDataSource<NumberSection>()
4947

50-
skinTableViewDataSource(tvAnimatedDataSource)
51-
skinTableViewDataSource(reloadDataSource)
48+
let (configureCell, titleForSection) = ViewController.tableViewDataSourceUI()
49+
let tvAnimatedDataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>(
50+
configureCell: configureCell,
51+
titleForHeaderInSection: titleForSection
52+
)
53+
let reloadDataSource = RxTableViewSectionedReloadDataSource<NumberSection>(
54+
configureCell: configureCell,
55+
titleForHeaderInSection: titleForSection
56+
)
5257

5358
randomSections
5459
.bind(to: animatedTableView.rx.items(dataSource: tvAnimatedDataSource))
@@ -58,8 +63,11 @@ class ViewController: UIViewController {
5863
.bind(to: tableView.rx.items(dataSource: reloadDataSource))
5964
.addDisposableTo(disposeBag)
6065

61-
let cvAnimatedDataSource = RxCollectionViewSectionedAnimatedDataSource<NumberSection>()
62-
skinCollectionViewDataSource(cvAnimatedDataSource)
66+
let (configureCollectionViewCell, configureSupplementaryView) = ViewController.collectionViewDataSourceUI()
67+
let cvAnimatedDataSource = RxCollectionViewSectionedAnimatedDataSource(
68+
configureCell: configureCollectionViewCell,
69+
configureSupplementaryView: configureSupplementaryView
70+
)
6371

6472
randomSections
6573
.bind(to: animatedCollectionView.rx.items(dataSource: cvAnimatedDataSource))
@@ -83,36 +91,39 @@ class ViewController: UIViewController {
8391
// MARK: Skinning
8492
extension ViewController {
8593

86-
func skinTableViewDataSource(_ dataSource: TableViewSectionedDataSource<NumberSection>) {
87-
dataSource.configureCell = { (_, tv, ip, i) in
88-
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style:.default, reuseIdentifier: "Cell")
89-
90-
cell.textLabel!.text = "\(i)"
91-
92-
return cell
93-
}
94-
95-
dataSource.titleForHeaderInSection = { (ds, section) -> String? in
96-
return ds[section].header
97-
}
94+
static func tableViewDataSourceUI() -> (
95+
TableViewSectionedDataSource<NumberSection>.ConfigureCell,
96+
TableViewSectionedDataSource<NumberSection>.TitleForHeaderInSection
97+
) {
98+
return (
99+
{ (_, tv, ip, i) in
100+
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style:.default, reuseIdentifier: "Cell")
101+
cell.textLabel!.text = "\(i)"
102+
return cell
103+
},
104+
{ (ds, section) -> String? in
105+
return ds[section].header
106+
}
107+
)
98108
}
99109

100-
func skinCollectionViewDataSource(_ dataSource: CollectionViewSectionedDataSource<NumberSection>) {
101-
dataSource.configureCell = { (_, cv, ip, i) in
102-
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: ip) as! NumberCell
103-
104-
cell.value!.text = "\(i)"
105-
106-
return cell
107-
}
108-
109-
dataSource.supplementaryViewFactory = { (ds ,cv, kind, ip) in
110-
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip) as! NumberSectionView
111-
112-
section.value!.text = "\(ds[ip.section].header)"
113-
114-
return section
115-
}
110+
static func collectionViewDataSourceUI() -> (
111+
CollectionViewSectionedDataSource<NumberSection>.ConfigureCell,
112+
CollectionViewSectionedDataSource<NumberSection>.ConfigureSupplementaryView
113+
) {
114+
return (
115+
{ (_, cv, ip, i) in
116+
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: ip) as! NumberCell
117+
cell.value!.text = "\(i)"
118+
return cell
119+
120+
},
121+
{ (ds ,cv, kind, ip) in
122+
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip) as! NumberSectionView
123+
section.value!.text = "\(ds[ip.section].header)"
124+
return section
125+
}
126+
)
116127
}
117128

118129
// MARK: Initial value

Example/Example3_TableViewEditing.swift

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class EditingExampleViewController: UIViewController {
2222
override func viewDidLoad() {
2323
super.viewDidLoad()
2424

25-
let dataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>()
25+
let dataSource = EditingExampleViewController.dataSource()
26+
2627
let sections: [NumberSection] = [NumberSection(header: "Section 1", numbers: [], updated: Date()),
2728
NumberSection(header: "Section 2", numbers: [], updated: Date()),
2829
NumberSection(header: "Section 3", numbers: [], updated: Date())]
@@ -39,7 +40,6 @@ class EditingExampleViewController: UIViewController {
3940
let movedCommand = tableView.rx.itemMoved
4041
.map(TableViewEditingCommand.MoveItem)
4142

42-
skinTableViewDataSource(dataSource: dataSource)
4343
Observable.of(addCommand, deleteCommand, movedCommand)
4444
.merge()
4545
.scan(initialState) { (state: SectionedTableViewState, command: TableViewEditingCommand) -> SectionedTableViewState in
@@ -58,31 +58,29 @@ class EditingExampleViewController: UIViewController {
5858
super.viewDidAppear(animated)
5959
tableView.setEditing(true, animated: true)
6060
}
61-
62-
func skinTableViewDataSource(dataSource: RxTableViewSectionedAnimatedDataSource<NumberSection>) {
63-
64-
dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: .top,
61+
}
62+
63+
extension EditingExampleViewController {
64+
static func dataSource() -> RxTableViewSectionedAnimatedDataSource<NumberSection> {
65+
return RxTableViewSectionedAnimatedDataSource(
66+
animationConfiguration: AnimationConfiguration(insertAnimation: .top,
6567
reloadAnimation: .fade,
66-
deleteAnimation: .left)
67-
68-
dataSource.configureCell = { (dataSource, table, idxPath, item) in
69-
let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)
70-
71-
cell.textLabel?.text = "\(item)"
72-
73-
return cell
74-
}
75-
76-
dataSource.titleForHeaderInSection = { (ds, section) -> String? in
77-
return ds[section].header
78-
}
79-
80-
dataSource.canEditRowAtIndexPath = { _, _ in
81-
return true
82-
}
83-
dataSource.canMoveRowAtIndexPath = { _, _ in
84-
return true
85-
}
68+
deleteAnimation: .left),
69+
configureCell: { (dataSource, table, idxPath, item) in
70+
let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)
71+
cell.textLabel?.text = "\(item)"
72+
return cell
73+
},
74+
titleForHeaderInSection: { (ds, section) -> String? in
75+
return ds[section].header
76+
},
77+
canEditRowAtIndexPath: { _, _ in
78+
return true
79+
},
80+
canMoveRowAtIndexPath: { _, _ in
81+
return true
82+
}
83+
)
8684
}
8785
}
8886

Example/Example4_DifferentSectionAndItemTypes.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,43 @@ class MultipleSectionModelViewController: UIViewController {
2929
items: [.StepperSectionItem(title: "1")])
3030
]
3131

32-
let dataSource = RxTableViewSectionedReloadDataSource<MultipleSectionModel>()
33-
34-
skinTableViewDataSource(dataSource)
32+
let dataSource = MultipleSectionModelViewController.dataSource()
3533

3634
Observable.just(sections)
3735
.bind(to: tableView.rx.items(dataSource: dataSource))
3836
.addDisposableTo(disposeBag)
3937
}
40-
41-
func skinTableViewDataSource(_ dataSource: RxTableViewSectionedReloadDataSource<MultipleSectionModel>) {
42-
dataSource.configureCell = { (dataSource, table, idxPath, _) in
43-
switch dataSource[idxPath] {
44-
case let .ImageSectionItem(image, title):
45-
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
46-
cell.titleLabel.text = title
47-
cell.cellImageView.image = image
48-
49-
return cell
50-
case let .StepperSectionItem(title):
51-
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
52-
cell.titleLabel.text = title
53-
54-
return cell
55-
case let .ToggleableSectionItem(title, enabled):
56-
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
57-
cell.switchControl.isOn = enabled
58-
cell.titleLabel.text = title
59-
60-
return cell
61-
}
62-
}
38+
}
6339

64-
dataSource.titleForHeaderInSection = { dataSource, index in
65-
let section = dataSource[index]
66-
67-
return section.title
68-
}
40+
extension MultipleSectionModelViewController {
41+
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
42+
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
43+
configureCell: { (dataSource, table, idxPath, _) in
44+
switch dataSource[idxPath] {
45+
case let .ImageSectionItem(image, title):
46+
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
47+
cell.titleLabel.text = title
48+
cell.cellImageView.image = image
49+
50+
return cell
51+
case let .StepperSectionItem(title):
52+
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
53+
cell.titleLabel.text = title
54+
55+
return cell
56+
case let .ToggleableSectionItem(title, enabled):
57+
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
58+
cell.switchControl.isOn = enabled
59+
cell.titleLabel.text = title
60+
61+
return cell
62+
}
63+
},
64+
titleForHeaderInSection: { dataSource, index in
65+
let section = dataSource[index]
66+
return section.title
67+
}
68+
)
6969
}
7070
}
7171

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
C82C3C961F3B939100309AE8 /* SectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C701F3B938C00309AE8 /* SectionModel.swift */; };
4949
C82C3C971F3B939100309AE8 /* SectionModelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C711F3B938C00309AE8 /* SectionModelType.swift */; };
5050
C82C3C991F3B939100309AE8 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C731F3B938C00309AE8 /* Utilities.swift */; };
51+
C833338D1F8A5FAC00D46EAE /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = C833338C1F8A5FAC00D46EAE /* Deprecated.swift */; };
5152
C87C34991F363B1400DB85FE /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; };
5253
C87C349A1F363B1400DB85FE /* RxSwift.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
5354
C87C349D1F363B1400DB85FE /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C343A1F36346A00DB85FE /* RxCocoa.framework */; };
@@ -365,6 +366,7 @@
365366
C82C3C701F3B938C00309AE8 /* SectionModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModel.swift; sourceTree = "<group>"; };
366367
C82C3C711F3B938C00309AE8 /* SectionModelType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModelType.swift; sourceTree = "<group>"; };
367368
C82C3C731F3B938C00309AE8 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
369+
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Deprecated.swift; sourceTree = "<group>"; };
368370
C861C0F01E153FC400BEDC46 /* RxDataSources.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = RxDataSources.podspec; sourceTree = "<group>"; };
369371
C87C34191F36346A00DB85FE /* Rx.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Rx.xcodeproj; path = RxSwift/Rx.xcodeproj; sourceTree = "<group>"; };
370372
C87DF3431D0219A7006308C5 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -663,6 +665,7 @@
663665
C8BBFBD41F3B8F8D00A225F7 /* RxTableViewSectionedReloadDataSource.swift */,
664666
C8BBFBD51F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift */,
665667
C8BBFBD61F3B8F8D00A225F7 /* UI+SectionedViewType.swift */,
668+
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */,
666669
);
667670
path = RxDataSources;
668671
sourceTree = "<group>";
@@ -1132,6 +1135,7 @@
11321135
C8BBFBE01F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift in Sources */,
11331136
C8BBFBE11F3B8F8D00A225F7 /* UI+SectionedViewType.swift in Sources */,
11341137
C8BBFBDB1F3B8F8D00A225F7 /* RxCollectionViewSectionedReloadDataSource.swift in Sources */,
1138+
C833338D1F8A5FAC00D46EAE /* Deprecated.swift in Sources */,
11351139
C81FBF631F3B9DC00094061E /* IntegerType+IdentifiableType.swift in Sources */,
11361140
C8BBFBDD1F3B8F8D00A225F7 /* RxPickerViewAdapter.swift in Sources */,
11371141
);

Sources/RxDataSources/AnimationConfiguration.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
//
88

99
#if os(iOS) || os(tvOS)
10-
import Foundation
11-
import UIKit
10+
import Foundation
11+
import UIKit
1212

13-
/**
14-
Exposes custom animation styles for insertion, deletion and reloading behavior.
15-
*/
16-
public struct AnimationConfiguration {
17-
public let insertAnimation: UITableViewRowAnimation
18-
public let reloadAnimation: UITableViewRowAnimation
19-
public let deleteAnimation: UITableViewRowAnimation
20-
21-
public init(insertAnimation: UITableViewRowAnimation = .automatic,
22-
reloadAnimation: UITableViewRowAnimation = .automatic,
23-
deleteAnimation: UITableViewRowAnimation = .automatic) {
24-
self.insertAnimation = insertAnimation
25-
self.reloadAnimation = reloadAnimation
26-
self.deleteAnimation = deleteAnimation
27-
}
28-
}
13+
/**
14+
Exposes custom animation styles for insertion, deletion and reloading behavior.
15+
*/
16+
public struct AnimationConfiguration {
17+
public let insertAnimation: UITableViewRowAnimation
18+
public let reloadAnimation: UITableViewRowAnimation
19+
public let deleteAnimation: UITableViewRowAnimation
20+
21+
public init(insertAnimation: UITableViewRowAnimation = .automatic,
22+
reloadAnimation: UITableViewRowAnimation = .automatic,
23+
deleteAnimation: UITableViewRowAnimation = .automatic) {
24+
self.insertAnimation = insertAnimation
25+
self.reloadAnimation = reloadAnimation
26+
self.deleteAnimation = deleteAnimation
27+
}
28+
}
2929
#endif

0 commit comments

Comments
 (0)