Skip to content

Commit af71f4f

Browse files
committed
Improved heterogeneous sections example.
1 parent db4f7a9 commit af71f4f

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

Example/MultipleSectionModelViewController.swift

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,21 @@ import RxCocoa
1212
import RxSwift
1313

1414
enum MultipleSectionModel {
15-
case ImageProvidableSection(title: String, items: [ImageSectionItem])
16-
case ToggleableSection(title: String, items: [ToggleableSectionItem])
17-
case StepperableSection(title: String, items: [StepperSectionItem])
15+
case ImageProvidableSection(title: String, items: [SectionItem])
16+
case ToggleableSection(title: String, items: [SectionItem])
17+
case StepperableSection(title: String, items: [SectionItem])
1818
}
1919

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-
}
20+
enum SectionItem {
21+
case ImageSectionItem(image: UIImage, title: String)
22+
case ToggleableSectionItem(title: String, enabled: Bool)
23+
case StepperSectionItem(title: String)
3124
}
3225

3326
extension MultipleSectionModel: SectionModelType {
34-
typealias Item = Any
27+
typealias Item = SectionItem
3528

36-
var items: [Item] {
29+
var items: [SectionItem] {
3730
switch self {
3831
case .ImageProvidableSection(title: _, items: let items):
3932
return items.map {$0}
@@ -49,20 +42,6 @@ extension MultipleSectionModel: SectionModelType {
4942
}
5043
}
5144

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-
6645
class MultipleSectionModelViewController: UIViewController {
6746

6847
@IBOutlet weak var tableView: UITableView!
@@ -73,11 +52,11 @@ class MultipleSectionModelViewController: UIViewController {
7352

7453
let sections: [MultipleSectionModel] = [
7554
.ImageProvidableSection(title: "Section 1",
76-
items: [ImageSectionItem(image: UIImage(named: "settings")!, title: "General")]),
55+
items: [.ImageSectionItem(image: UIImage(named: "settings")!, title: "General")]),
7756
.ToggleableSection(title: "Section 2",
78-
items: [ToggleableSectionItem(title: "On", enabled: true)]),
57+
items: [.ToggleableSectionItem(title: "On", enabled: true)]),
7958
.StepperableSection(title: "Section 3",
80-
items: [StepperSectionItem(title: "1")])
59+
items: [.StepperSectionItem(title: "1")])
8160
]
8261

8362
let dataSource = RxTableViewSectionedReloadDataSource<MultipleSectionModel>()
@@ -90,25 +69,22 @@ class MultipleSectionModelViewController: UIViewController {
9069

9170
func skinTableViewDataSource(dataSource: RxTableViewSectionedReloadDataSource<MultipleSectionModel>) {
9271
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]
72+
switch dataSource.itemAtIndexPath(idxPath) {
73+
case let .ImageSectionItem(image, title):
9674
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
97-
cell.titleLabel.text = item.title
98-
cell.cellImageView.image = item.image
75+
cell.titleLabel.text = title
76+
cell.cellImageView.image = image
9977

10078
return cell
101-
case .StepperableSection(title: _, items: let items):
102-
let item = items[idxPath.row]
79+
case let .StepperSectionItem(title):
10380
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
104-
cell.titleLabel.text = item.title
81+
cell.titleLabel.text = title
10582

10683
return cell
107-
case .ToggleableSection(title: _, items: let items):
108-
let item = items[idxPath.row]
84+
case let .ToggleableSectionItem(title, enabled):
10985
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
110-
cell.switchControl.on = item.enabled
111-
cell.titleLabel.text = item.title
86+
cell.switchControl.on = enabled
87+
cell.titleLabel.text = title
11288

11389
return cell
11490
}
@@ -122,3 +98,16 @@ class MultipleSectionModelViewController: UIViewController {
12298
}
12399

124100
}
101+
102+
extension MultipleSectionModel {
103+
var title: String {
104+
switch self {
105+
case .ImageProvidableSection(title: let title, items: _):
106+
return title
107+
case .StepperableSection(title: let title, items: _):
108+
return title
109+
case .ToggleableSection(title: let title, items: _):
110+
return title
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)