-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathExample4_DifferentSectionAndItemTypes.swift
117 lines (103 loc) · 4.14 KB
/
Example4_DifferentSectionAndItemTypes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// MultipleSectionModelViewController.swift
// RxDataSources
//
// Created by Segii Shulga on 4/26/16.
// Copyright © 2016 kzaher. All rights reserved.
//
import UIKit
import RxDataSources
import RxCocoa
import RxSwift
import Differentiator
// the trick is to just use enum for different section types
class MultipleSectionModelViewController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let sections: [MultipleSectionModel] = [
.ImageProvidableSection(title: "Section 1",
items: [.ImageSectionItem(image: UIImage(named: "settings")!, title: "General")]),
.ToggleableSection(title: "Section 2",
items: [.ToggleableSectionItem(title: "On", enabled: true)]),
.StepperableSection(title: "Section 3",
items: [.StepperSectionItem(title: "1")])
]
let dataSource = MultipleSectionModelViewController.dataSource()
Observable.just(sections)
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
}
extension MultipleSectionModelViewController {
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
configureCell: { dataSource, table, idxPath, _ in
switch dataSource[idxPath] {
case let .ImageSectionItem(image, title):
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.configure(image: image, title: title)
return cell
case let .StepperSectionItem(title):
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.configure(title: title)
return cell
case let .ToggleableSectionItem(title, enabled):
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.configure(title: title, isEnabled: enabled)
return cell
}
},
titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
return section.title
}
)
}
}
enum MultipleSectionModel {
case ImageProvidableSection(title: String, items: [SectionItem])
case ToggleableSection(title: String, items: [SectionItem])
case StepperableSection(title: String, items: [SectionItem])
}
enum SectionItem {
case ImageSectionItem(image: UIImage, title: String)
case ToggleableSectionItem(title: String, enabled: Bool)
case StepperSectionItem(title: String)
}
extension MultipleSectionModel: SectionModelType {
typealias Item = SectionItem
var items: [SectionItem] {
switch self {
case .ImageProvidableSection(title: _, items: let items):
return items.map { $0 }
case .StepperableSection(title: _, items: let items):
return items.map { $0 }
case .ToggleableSection(title: _, items: let items):
return items.map { $0 }
}
}
init(original: MultipleSectionModel, items: [Item]) {
switch original {
case let .ImageProvidableSection(title: title, items: _):
self = .ImageProvidableSection(title: title, items: items)
case let .StepperableSection(title, _):
self = .StepperableSection(title: title, items: items)
case let .ToggleableSection(title, _):
self = .ToggleableSection(title: title, items: items)
}
}
}
extension MultipleSectionModel {
var title: String {
switch self {
case .ImageProvidableSection(title: let title, items: _):
return title
case .StepperableSection(title: let title, items: _):
return title
case .ToggleableSection(title: let title, items: _):
return title
}
}
}