-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathExample1_CustomizationUsingTableViewDelegate.swift
93 lines (74 loc) · 2.35 KB
/
Example1_CustomizationUsingTableViewDelegate.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
//
// CustomizationUsingTableViewDelegate.swift
// RxDataSources
//
// Created by Krunoslav Zaher on 4/19/16.
// Copyright © 2016 kzaher. All rights reserved.
//
import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import Differentiator
struct MySection {
var header: String
var items: [Item]
}
extension MySection : AnimatableSectionModelType {
typealias Item = Int
var identity: String {
return header
}
init(original: MySection, items: [Item]) {
self = original
self.items = items
}
}
class CustomizationUsingTableViewDelegate : UIViewController {
@IBOutlet private var tableView: UITableView!
let disposeBag = DisposeBag()
var dataSource: RxTableViewSectionedAnimatedDataSource<MySection>?
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>(
configureCell: { ds, tv, _, item in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = "Item \(item)"
return cell
},
titleForHeaderInSection: { ds, index in
return ds.sectionModels[index].header
}
)
self.dataSource = dataSource
let sections = [
MySection(header: "First section", items: [
1,
2
]),
MySection(header: "Second section", items: [
3,
4
])
]
Observable.just(sections)
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
tableView.rx.setDelegate(self)
.disposed(by: disposeBag)
}
}
extension CustomizationUsingTableViewDelegate : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// you can also fetch item
guard let item = dataSource?[indexPath],
// .. or section and customize what you like
dataSource?[indexPath.section] != nil
else {
return 0.0
}
return CGFloat(40 + item * 10)
}
}