@@ -10,9 +10,9 @@ import UIKit
10
10
11
11
open class CollapseTableView : UITableView {
12
12
///
13
- private weak var collapseDataSource : UITableViewDataSource !
13
+ private weak var collapseDataSource : UITableViewDataSource ?
14
14
///
15
- private weak var collapseDelegate : UITableViewDelegate !
15
+ private weak var collapseDelegate : UITableViewDelegate ?
16
16
/// Represents opened/closed states of tableView's sections.
17
17
private( set) var sectionStates = [ Bool] ( )
18
18
/// Determines, if section's headerView can be clickable.
@@ -42,10 +42,10 @@ open class CollapseTableView: UITableView {
42
42
}
43
43
44
44
override open func forwardingTarget( for aSelector: Selector ! ) -> Any ? {
45
- if collapseDataSource. responds ( to: aSelector) {
45
+ if collapseDataSource? . responds ( to: aSelector) ?? false {
46
46
return collapseDataSource
47
47
}
48
- if collapseDelegate. responds ( to: aSelector) {
48
+ if collapseDelegate? . responds ( to: aSelector) ?? false {
49
49
return collapseDelegate
50
50
}
51
51
return nil
@@ -125,6 +125,9 @@ open class CollapseTableView: UITableView {
125
125
}
126
126
127
127
private func indexPathsForRowsInSectionAtIndex( _ sectionIndex: Int ) -> [ IndexPath ] ? {
128
+ guard let collapseDataSource = collapseDataSource else {
129
+ return nil
130
+ }
128
131
if sectionIndex >= sectionStates. count {
129
132
return nil
130
133
}
@@ -148,7 +151,7 @@ open class CollapseTableView: UITableView {
148
151
149
152
extension CollapseTableView : UITableViewDataSource {
150
153
public func numberOfSections( in tableView: UITableView ) -> Int {
151
- let numberOfSections = collapseDataSource. numberOfSections ? ( in: tableView) ?? 0
154
+ let numberOfSections = collapseDataSource? . numberOfSections ? ( in: tableView) ?? 0
152
155
while numberOfSections < sectionStates. count {
153
156
sectionStates. removeAll ( )
154
157
}
@@ -160,21 +163,21 @@ extension CollapseTableView: UITableViewDataSource {
160
163
161
164
public func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
162
165
if sectionStates [ section] {
163
- return collapseDataSource. tableView ( tableView, numberOfRowsInSection: section)
166
+ return collapseDataSource? . tableView ( tableView, numberOfRowsInSection: section) ?? 0
164
167
}
165
168
return 0
166
169
}
167
170
168
171
public func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
169
- return collapseDataSource. tableView ( tableView, cellForRowAt: indexPath)
172
+ return collapseDataSource? . tableView ( tableView, cellForRowAt: indexPath) ?? UITableViewCell ( )
170
173
}
171
174
}
172
175
173
176
// MARK: UITableViewDelegate
174
177
175
178
extension CollapseTableView : UITableViewDelegate {
176
179
public func tableView( _ tableView: UITableView , viewForHeaderInSection section: Int ) -> UIView ? {
177
- guard let view = collapseDelegate. tableView ? ( tableView, viewForHeaderInSection: section) else {
180
+ guard let view = collapseDelegate? . tableView ? ( tableView, viewForHeaderInSection: section) else {
178
181
return nil
179
182
}
180
183
if shouldHandleHeadersTap {
0 commit comments