@@ -10,9 +10,9 @@ import UIKit
1010
1111open class CollapseTableView : UITableView {
1212 ///
13- private weak var collapseDataSource : UITableViewDataSource !
13+ private weak var collapseDataSource : UITableViewDataSource ?
1414 ///
15- private weak var collapseDelegate : UITableViewDelegate !
15+ private weak var collapseDelegate : UITableViewDelegate ?
1616 /// Represents opened/closed states of tableView's sections.
1717 private( set) var sectionStates = [ Bool] ( )
1818 /// Determines, if section's headerView can be clickable.
@@ -42,10 +42,10 @@ open class CollapseTableView: UITableView {
4242 }
4343
4444 override open func forwardingTarget( for aSelector: Selector ! ) -> Any ? {
45- if collapseDataSource. responds ( to: aSelector) {
45+ if collapseDataSource? . responds ( to: aSelector) ?? false {
4646 return collapseDataSource
4747 }
48- if collapseDelegate. responds ( to: aSelector) {
48+ if collapseDelegate? . responds ( to: aSelector) ?? false {
4949 return collapseDelegate
5050 }
5151 return nil
@@ -125,6 +125,9 @@ open class CollapseTableView: UITableView {
125125 }
126126
127127 private func indexPathsForRowsInSectionAtIndex( _ sectionIndex: Int ) -> [ IndexPath ] ? {
128+ guard let collapseDataSource = collapseDataSource else {
129+ return nil
130+ }
128131 if sectionIndex >= sectionStates. count {
129132 return nil
130133 }
@@ -148,7 +151,7 @@ open class CollapseTableView: UITableView {
148151
149152extension CollapseTableView : UITableViewDataSource {
150153 public func numberOfSections( in tableView: UITableView ) -> Int {
151- let numberOfSections = collapseDataSource. numberOfSections ? ( in: tableView) ?? 0
154+ let numberOfSections = collapseDataSource? . numberOfSections ? ( in: tableView) ?? 0
152155 while numberOfSections < sectionStates. count {
153156 sectionStates. removeAll ( )
154157 }
@@ -160,21 +163,21 @@ extension CollapseTableView: UITableViewDataSource {
160163
161164 public func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
162165 if sectionStates [ section] {
163- return collapseDataSource. tableView ( tableView, numberOfRowsInSection: section)
166+ return collapseDataSource? . tableView ( tableView, numberOfRowsInSection: section) ?? 0
164167 }
165168 return 0
166169 }
167170
168171 public func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
169- return collapseDataSource. tableView ( tableView, cellForRowAt: indexPath)
172+ return collapseDataSource? . tableView ( tableView, cellForRowAt: indexPath) ?? UITableViewCell ( )
170173 }
171174}
172175
173176// MARK: UITableViewDelegate
174177
175178extension CollapseTableView : UITableViewDelegate {
176179 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 {
178181 return nil
179182 }
180183 if shouldHandleHeadersTap {
0 commit comments