Skip to content

Commit

Permalink
Make collapse delegate and datasource optional
Browse files Browse the repository at this point in the history
This change was required due to reported amount of crashes causing by force unwrapped delegate

Increase version number

Update podspec file
  • Loading branch information
Kharauzov committed Aug 1, 2020
1 parent dcf1830 commit 95126e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CollapseTableView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ s.ios.deployment_target = '10.0'
s.name = "CollapseTableView"
s.summary = "CollapseTableView enables to expand sections with cells inside."
s.requires_arc = true
s.version = "1.0.2"
s.version = "1.0.3"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Serhii Kharauzov" => "serhii.kharauzov@gmail.com" }
s.homepage = "https://github.com/Kharauzov/CollapseTableView"
Expand Down
4 changes: 4 additions & 0 deletions CollapseTableView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,12 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.3;
PRODUCT_BUNDLE_IDENTIFIER = sk.CollapseTableView;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PROVISIONING_PROFILE_SPECIFIER = "";
SKIP_INSTALL = YES;
SUPPORTS_MACCATALYST = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -503,10 +505,12 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.3;
PRODUCT_BUNDLE_IDENTIFIER = sk.CollapseTableView;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
PROVISIONING_PROFILE_SPECIFIER = "";
SKIP_INSTALL = YES;
SUPPORTS_MACCATALYST = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand Down
2 changes: 1 addition & 1 deletion CollapseTableView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
Expand Down
19 changes: 11 additions & 8 deletions CollapseTableView/Source/CollapseTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import UIKit

open class CollapseTableView: UITableView {
///
private weak var collapseDataSource: UITableViewDataSource!
private weak var collapseDataSource: UITableViewDataSource?
///
private weak var collapseDelegate: UITableViewDelegate!
private weak var collapseDelegate: UITableViewDelegate?
/// Represents opened/closed states of tableView's sections.
private(set) var sectionStates = [Bool]()
/// Determines, if section's headerView can be clickable.
Expand Down Expand Up @@ -42,10 +42,10 @@ open class CollapseTableView: UITableView {
}

override open func forwardingTarget(for aSelector: Selector!) -> Any? {
if collapseDataSource.responds(to: aSelector) {
if collapseDataSource?.responds(to: aSelector) ?? false {
return collapseDataSource
}
if collapseDelegate.responds(to: aSelector) {
if collapseDelegate?.responds(to: aSelector) ?? false {
return collapseDelegate
}
return nil
Expand Down Expand Up @@ -125,6 +125,9 @@ open class CollapseTableView: UITableView {
}

private func indexPathsForRowsInSectionAtIndex(_ sectionIndex: Int) -> [IndexPath]? {
guard let collapseDataSource = collapseDataSource else {
return nil
}
if sectionIndex >= sectionStates.count {
return nil
}
Expand All @@ -148,7 +151,7 @@ open class CollapseTableView: UITableView {

extension CollapseTableView: UITableViewDataSource {
public func numberOfSections(in tableView: UITableView) -> Int {
let numberOfSections = collapseDataSource.numberOfSections?(in: tableView) ?? 0
let numberOfSections = collapseDataSource?.numberOfSections?(in: tableView) ?? 0
while numberOfSections < sectionStates.count {
sectionStates.removeAll()
}
Expand All @@ -160,21 +163,21 @@ extension CollapseTableView: UITableViewDataSource {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if sectionStates[section] {
return collapseDataSource.tableView(tableView, numberOfRowsInSection: section)
return collapseDataSource?.tableView(tableView, numberOfRowsInSection: section) ?? 0
}
return 0
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return collapseDataSource.tableView(tableView, cellForRowAt: indexPath)
return collapseDataSource?.tableView(tableView, cellForRowAt: indexPath) ?? UITableViewCell()
}
}

// MARK: UITableViewDelegate

extension CollapseTableView: UITableViewDelegate {
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let view = collapseDelegate.tableView?(tableView, viewForHeaderInSection: section) else {
guard let view = collapseDelegate?.tableView?(tableView, viewForHeaderInSection: section) else {
return nil
}
if shouldHandleHeadersTap {
Expand Down

0 comments on commit 95126e8

Please sign in to comment.