Skip to content

Commit 489afc1

Browse files
committed
Added multiple section model example
1 parent 3928d62 commit 489afc1

File tree

10 files changed

+422
-3
lines changed

10 files changed

+422
-3
lines changed

Example/Assets.xcassets/Contents.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "[email protected]",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
3.52 KB
Loading

Example/Base.lproj/Main.storyboard

Lines changed: 151 additions & 3 deletions
Large diffs are not rendered by default.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//
2+
// MultipleSectionModelViewController.swift
3+
// RxDataSources
4+
//
5+
// Created by Segii Shulga on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import RxDataSources
11+
import RxCocoa
12+
import RxSwift
13+
14+
enum MultipleSectionModel {
15+
case ImageProvidableSection(title: String, items: [ImageSectionItem])
16+
case ToggleableSection(title: String, items: [ToggleableSectionItem])
17+
case StepperableSection(title: String, items: [StepperSectionItem])
18+
}
19+
20+
extension MultipleSectionModel {
21+
var title: String {
22+
switch self {
23+
case .ImageProvidableSection(title: let title, items: _):
24+
return title
25+
case .StepperableSection(title: let title, items: _):
26+
return title
27+
case .ToggleableSection(title: let title, items: _):
28+
return title
29+
}
30+
}
31+
}
32+
33+
extension MultipleSectionModel: SectionModelType {
34+
typealias Item = Any
35+
36+
var items: [Item] {
37+
switch self {
38+
case .ImageProvidableSection(title: _, items: let items):
39+
return items.map {$0}
40+
case .StepperableSection(title: _, items: let items):
41+
return items.map {$0}
42+
case .ToggleableSection(title: _, items: let items):
43+
return items.map {$0}
44+
}
45+
}
46+
47+
init(original: MultipleSectionModel, items: [Item]) {
48+
self = original
49+
}
50+
}
51+
52+
struct ImageSectionItem {
53+
let image: UIImage
54+
let title: String
55+
}
56+
57+
struct ToggleableSectionItem {
58+
let title: String
59+
let enabled: Bool
60+
}
61+
62+
struct StepperSectionItem {
63+
let title: String
64+
}
65+
66+
class MultipleSectionModelViewController: UIViewController {
67+
68+
@IBOutlet weak var tableView: UITableView!
69+
let disposeBag = DisposeBag()
70+
71+
override func viewDidLoad() {
72+
super.viewDidLoad()
73+
74+
let sections: [MultipleSectionModel] = [
75+
.ImageProvidableSection(title: "Section 1",
76+
items: [ImageSectionItem(image: UIImage(named: "settings")!, title: "General")]),
77+
.ToggleableSection(title: "Section 2",
78+
items: [ToggleableSectionItem(title: "On", enabled: true)]),
79+
.StepperableSection(title: "Section 3",
80+
items: [StepperSectionItem(title: "1")])
81+
]
82+
83+
let dataSource = RxTableViewSectionedReloadDataSource<MultipleSectionModel>()
84+
Observable.just(sections)
85+
.bindTo(tableView.rx_itemsWithDataSource(dataSource))
86+
.addDisposableTo(disposeBag)
87+
88+
skinTableViewDataSource(dataSource)
89+
}
90+
91+
func skinTableViewDataSource(dataSource: RxTableViewSectionedReloadDataSource<MultipleSectionModel>) {
92+
dataSource.configureCell = { (dataSource, table, idxPath, _) in
93+
switch dataSource.sectionAtIndex(idxPath.section) {
94+
case .ImageProvidableSection(title: _, items: let items):
95+
let item = items[idxPath.row]
96+
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
97+
cell.titleLabel.text = item.title
98+
cell.cellImageView.image = item.image
99+
100+
return cell
101+
case .StepperableSection(title: _, items: let items):
102+
let item = items[idxPath.row]
103+
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
104+
cell.titleLabel.text = item.title
105+
106+
return cell
107+
case .ToggleableSection(title: _, items: let items):
108+
let item = items[idxPath.row]
109+
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
110+
cell.switchControl.on = item.enabled
111+
cell.titleLabel.text = item.title
112+
113+
return cell
114+
}
115+
}
116+
117+
dataSource.titleForHeaderInSection = { dataSource, index in
118+
let section = dataSource.sectionAtIndex(index)
119+
120+
return section.title
121+
}
122+
}
123+
124+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// ImageTitleTableViewCell.swift
3+
// RxDataSources
4+
//
5+
// Created by Segii Shulga on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class ImageTitleTableViewCell: UITableViewCell {
12+
13+
@IBOutlet weak var cellImageView: UIImageView!
14+
@IBOutlet weak var titleLabel: UILabel!
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// TitleSteperTableViewCell.swift
3+
// RxDataSources
4+
//
5+
// Created by Segii Shulga on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class TitleSteperTableViewCell: UITableViewCell {
12+
13+
@IBOutlet weak var stepper: UIStepper!
14+
@IBOutlet weak var titleLabel: UILabel!
15+
override func awakeFromNib() {
16+
super.awakeFromNib()
17+
// Initialization code
18+
}
19+
20+
override func setSelected(selected: Bool, animated: Bool) {
21+
super.setSelected(selected, animated: animated)
22+
23+
// Configure the view for the selected state
24+
}
25+
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// TitleSwitchTableViewCell.swift
3+
// RxDataSources
4+
//
5+
// Created by Segii Shulga on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class TitleSwitchTableViewCell: UITableViewCell {
12+
13+
14+
@IBOutlet weak var switchControl: UISwitch!
15+
@IBOutlet weak var titleLabel: UILabel!
16+
}

Example/Views/UIKitExtensions.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// UIKitExtensions.swift
3+
// RxDataSources
4+
//
5+
// Created by Segii Shulga on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import class UIKit.UITableViewCell
10+
import class UIKit.UITableView
11+
import class Foundation.NSIndexPath
12+
13+
protocol ReusableView: class {
14+
static var reuseIdentifier: String {get}
15+
}
16+
17+
extension ReusableView {
18+
static var reuseIdentifier: String {
19+
return String(self)
20+
}
21+
}
22+
23+
extension UITableViewCell: ReusableView {
24+
}
25+
26+
extension UITableView {
27+
28+
func dequeueReusableCell<T: UITableViewCell where T: ReusableView>(forIndexPath indexPath: NSIndexPath) -> T {
29+
guard let cell = dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as? T else {
30+
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
31+
}
32+
33+
return cell
34+
}
35+
}

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
114F5F511C654D4100873440 /* AnimationConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 114F5F4E1C65480100873440 /* AnimationConfiguration.swift */; };
1313
14D4E327AC3FFD8D51339340 /* Pods_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F74935B588353100399C29D /* Pods_Example.framework */; };
1414
61253003A2D53E2633E68038 /* Pods_RxDataSources.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E39CF67E2A16306B47BCE935 /* Pods_RxDataSources.framework */; };
15+
845F61F41CCFD96800A8BE32 /* MultipleSectionModelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845F61F31CCFD96800A8BE32 /* MultipleSectionModelViewController.swift */; };
16+
845F61F71CCFDE6A00A8BE32 /* ImageTitleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845F61F61CCFDE6A00A8BE32 /* ImageTitleTableViewCell.swift */; };
17+
845F61F91CCFDEA800A8BE32 /* TitleSwitchTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845F61F81CCFDEA800A8BE32 /* TitleSwitchTableViewCell.swift */; };
18+
845F61FB1CCFDED600A8BE32 /* TitleSteperTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845F61FA1CCFDED600A8BE32 /* TitleSteperTableViewCell.swift */; };
19+
845F61FD1CCFF63600A8BE32 /* UIKitExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845F61FC1CCFF63600A8BE32 /* UIKitExtensions.swift */; };
1520
84FC6BEE1CA4830A00D3C605 /* EditingExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FC6BED1CA4830A00D3C605 /* EditingExampleViewController.swift */; };
1621
9FCDA16B1C3AF4A2000F5F94 /* Changeset.swift in Sources */ = {isa = PBXBuildFile; fileRef = C85EE5481C36F1FC0090614D /* Changeset.swift */; };
1722
9FCDA16C1C3AF4A2000F5F94 /* CollectionViewSectionedDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C85EE5491C36F1FC0090614D /* CollectionViewSectionedDataSource.swift */; };
@@ -168,6 +173,11 @@
168173
114F5F4E1C65480100873440 /* AnimationConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimationConfiguration.swift; sourceTree = "<group>"; };
169174
2A58DC421ECF08DEF551FDFB /* Pods-RxDataSources.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxDataSources.release.xcconfig"; path = "Pods/Target Support Files/Pods-RxDataSources/Pods-RxDataSources.release.xcconfig"; sourceTree = "<group>"; };
170175
2F74935B588353100399C29D /* Pods_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
176+
845F61F31CCFD96800A8BE32 /* MultipleSectionModelViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultipleSectionModelViewController.swift; sourceTree = "<group>"; };
177+
845F61F61CCFDE6A00A8BE32 /* ImageTitleTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageTitleTableViewCell.swift; sourceTree = "<group>"; };
178+
845F61F81CCFDEA800A8BE32 /* TitleSwitchTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleSwitchTableViewCell.swift; sourceTree = "<group>"; };
179+
845F61FA1CCFDED600A8BE32 /* TitleSteperTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleSteperTableViewCell.swift; sourceTree = "<group>"; };
180+
845F61FC1CCFF63600A8BE32 /* UIKitExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitExtensions.swift; sourceTree = "<group>"; };
171181
84FC6BED1CA4830A00D3C605 /* EditingExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditingExampleViewController.swift; sourceTree = "<group>"; };
172182
9FCDA1631C3AF49A000F5F94 /* RxDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; };
173183
9FF301481C4AA6D1007376BD /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/tvOS/RxCocoa.framework; sourceTree = "<group>"; };
@@ -287,6 +297,17 @@
287297
name = "Dependency Frameworks";
288298
sourceTree = "<group>";
289299
};
300+
845F61F51CCFDD7100A8BE32 /* Views */ = {
301+
isa = PBXGroup;
302+
children = (
303+
845F61F61CCFDE6A00A8BE32 /* ImageTitleTableViewCell.swift */,
304+
845F61F81CCFDEA800A8BE32 /* TitleSwitchTableViewCell.swift */,
305+
845F61FA1CCFDED600A8BE32 /* TitleSteperTableViewCell.swift */,
306+
845F61FC1CCFF63600A8BE32 /* UIKitExtensions.swift */,
307+
);
308+
path = Views;
309+
sourceTree = "<group>";
310+
};
290311
9F09B56DB2D6E8AB4933CC91 /* Pods */ = {
291312
isa = PBXGroup;
292313
children = (
@@ -414,6 +435,8 @@
414435
C8984C9D1C36B6FA001E4272 /* AppDelegate.swift */,
415436
C8984C9F1C36B6FA001E4272 /* ViewController.swift */,
416437
84FC6BED1CA4830A00D3C605 /* EditingExampleViewController.swift */,
438+
845F61F31CCFD96800A8BE32 /* MultipleSectionModelViewController.swift */,
439+
845F61F51CCFDD7100A8BE32 /* Views */,
417440
C8984CA11C36B6FA001E4272 /* Main.storyboard */,
418441
C8984CA41C36B6FA001E4272 /* Assets.xcassets */,
419442
C8984CA61C36B6FA001E4272 /* LaunchScreen.storyboard */,
@@ -818,11 +841,16 @@
818841
isa = PBXSourcesBuildPhase;
819842
buildActionMask = 2147483647;
820843
files = (
844+
845F61F71CCFDE6A00A8BE32 /* ImageTitleTableViewCell.swift in Sources */,
821845
C8BB763F1C4C3D45002B21C8 /* NumberSection.swift in Sources */,
822846
84FC6BEE1CA4830A00D3C605 /* EditingExampleViewController.swift in Sources */,
823847
C8984CA01C36B6FA001E4272 /* ViewController.swift in Sources */,
848+
845F61F41CCFD96800A8BE32 /* MultipleSectionModelViewController.swift in Sources */,
849+
845F61FB1CCFDED600A8BE32 /* TitleSteperTableViewCell.swift in Sources */,
824850
C8984CD81C36BED7001E4272 /* Randomizer.swift in Sources */,
851+
845F61F91CCFDEA800A8BE32 /* TitleSwitchTableViewCell.swift in Sources */,
825852
C8984C9E1C36B6FA001E4272 /* AppDelegate.swift in Sources */,
853+
845F61FD1CCFF63600A8BE32 /* UIKitExtensions.swift in Sources */,
826854
C8153A501CC6C2F40050C990 /* CustomizationUsingTableViewDelegate.swift in Sources */,
827855
);
828856
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)