Skip to content

Added Deselect Delegate for children cell #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Example/Views/AccordionTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ extension AccordionTableViewController {
}

let didSelectChildCell = { (tableView: UITableView, indexPath: IndexPath, item: CountryCellModel?) -> Void in
print("Child cell \(item!.name) tapped")
print("Child cell \(item!.name) selected")
}

let didDeselectChildCell = { (tableView: UITableView, indexPath: IndexPath, item: Marker?) -> Void in
print("Child cell \(item!.icon) deselected")
}

let scrollViewDidScroll = { (scrollView: UIScrollView) -> Void in
Expand All @@ -133,6 +137,7 @@ extension AccordionTableViewController {
childCellConfig: childCellConfig,
didSelectParentAtIndexPath: didSelectParentCell,
didSelectChildAtIndexPath: didSelectChildCell,
didDeselectChildAtIndexPath: didDeselectChildCell,
scrollViewDidScroll: scrollViewDidScroll,
// Configure DataSourceProvider to have only one parent expanded at a time
numberOfExpandedParentCells: .single
Expand Down
7 changes: 6 additions & 1 deletion Example/Views/AccordionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ extension AccordionViewController {
}

let didSelectChildCell = { (tableView: UITableView, indexPath: IndexPath, item: CountryCellModel?) -> Void in
print("Child cell \(item!.name) tapped")
print("Child cell \(item!.name) selected")
}

let didDeselectChildCell = { (tableView: UITableView, indexPath: IndexPath, item: Marker?) -> Void in
print("Child cell \(item!.name) deselected")
}

let heightForParentCell = { (tableView: UITableView, indexPath: IndexPath, item: ParentCellModel?) -> CGFloat in
Expand All @@ -137,6 +141,7 @@ extension AccordionViewController {
childCellConfig: childCellConfig,
didSelectParentAtIndexPath: didSelectParentCell,
didSelectChildAtIndexPath: didSelectChildCell,
didDeselectChildAtIndexPath: didDeselectChildCell,
heightForParentCellAtIndexPath: heightForParentCell,
heightForChildCellAtIndexPath: heightForChildCell,
scrollViewDidScroll: scrollViewDidScroll
Expand Down
20 changes: 20 additions & 0 deletions Source/DataSourceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class DataSourceProvider<DataSource: DataSourceType,

public typealias DidSelectParentAtIndexPathClosure = (UITableView, IndexPath, DataSource.Item?) -> Void
public typealias DidSelectChildAtIndexPathClosure = (UITableView, IndexPath, DataSource.Item.ChildItem?) -> Void
public typealias DidDeselectChildAtIndexPathClosure = (UITableView, IndexPath, DataSource.Item.ChildItem?) -> Void

public typealias HeightForChildAtIndexPathClosure = (UITableView, IndexPath, DataSource.Item.ChildItem?) -> CGFloat
public typealias HeightForParentAtIndexPathClosure = (UITableView, IndexPath, DataSource.Item?) -> CGFloat
Expand Down Expand Up @@ -59,6 +60,9 @@ public final class DataSourceProvider<DataSource: DataSourceType,
/// The closure to be called when a Child cell is selected
private let didSelectChildAtIndexPath: DidSelectChildAtIndexPathClosure?

/// The closure to be called when a Chile cell is deselected
private let didDeselectChildAtIndexPath: DidDeselectChildAtIndexPathClosure?

/// The closure to define the height of the Parent cell at the specified IndexPath
private let heightForParentCellAtIndexPath: HeightForParentAtIndexPathClosure?

Expand All @@ -80,6 +84,7 @@ public final class DataSourceProvider<DataSource: DataSourceType,
childCellConfig: ChildCellConfig,
didSelectParentAtIndexPath: DidSelectParentAtIndexPathClosure? = nil,
didSelectChildAtIndexPath: DidSelectChildAtIndexPathClosure? = nil,
didDeselectChildAtIndexPath: DidDeselectChildAtIndexPathClosure? = nil,
heightForParentCellAtIndexPath: HeightForParentAtIndexPathClosure? = nil,
heightForChildCellAtIndexPath: HeightForChildAtIndexPathClosure? = nil,
scrollViewDidScroll: ScrollViewDidScrollClosure? = nil,
Expand All @@ -90,6 +95,7 @@ public final class DataSourceProvider<DataSource: DataSourceType,
self.childCellConfig = childCellConfig
self.didSelectParentAtIndexPath = didSelectParentAtIndexPath
self.didSelectChildAtIndexPath = didSelectChildAtIndexPath
self.didDeselectChildAtIndexPath = didDeselectChildAtIndexPath
self.heightForParentCellAtIndexPath = heightForParentCellAtIndexPath
self.heightForChildCellAtIndexPath = heightForChildCellAtIndexPath
self.scrollViewDidScroll = scrollViewDidScroll
Expand Down Expand Up @@ -348,6 +354,20 @@ extension DataSourceProvider {
}
}

delegate.didDeselectRowAtIndexPath = { [unowned self] (tableView, indexPath) -> Void in
let (parentIndex, isParent, currentPosition) = self.dataSource.findParentOfCell(atIndexPath: indexPath)
let item = self.dataSource.item(atRow: parentIndex, inSection: indexPath.section)

if isParent {
self.update(tableView, item, currentPosition, indexPath, parentIndex)
self.didSelectParentAtIndexPath?(tableView, indexPath, item)
} else {
let index = indexPath.row - currentPosition - 1
let childItem = index >= 0 ? item?.children[index] : nil
self.didDeselectChildAtIndexPath?(tableView, indexPath, childItem)
}
}

delegate.heightForRowAtIndexPath = { [unowned self] (tableView, indexPath) -> CGFloat in
let (parentIndex, isParent, currentPosition) = self.dataSource.findParentOfCell(atIndexPath: indexPath)
let item = self.dataSource.item(atRow: parentIndex, inSection: indexPath.section)
Expand Down
6 changes: 6 additions & 0 deletions Source/TableViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import UIKit

typealias DidSelectRowAtIndexPathClosure = (UITableView, IndexPath) -> Void
typealias DidDeselectRowAtIntexPathClosure = (UITableView, IndexPath) -> Void
typealias HeightForRowAtIndexPathClosure = (UITableView, IndexPath) -> CGFloat
public typealias ScrollViewDidScrollClosure = (UIScrollView) -> Void

Expand All @@ -17,6 +18,7 @@ public typealias ScrollViewDidScrollClosure = (UIScrollView) -> Void
// MARK: - Properties

var didSelectRowAtIndexPath: DidSelectRowAtIndexPathClosure?
var didDeselectRowAtIndexPath: DidDeselectRowAtIntexPathClosure?
var heightForRowAtIndexPath: HeightForRowAtIndexPathClosure?

var scrollViewDidScrollClosure: ScrollViewDidScrollClosure?
Expand All @@ -28,6 +30,10 @@ extension TableViewDelegate: UITableViewDelegate {
didSelectRowAtIndexPath?(tableView, indexPath)
}

@objc func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
didDeselectRowAtIndexPath?(tableView, indexPath)
}

@objc func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return heightForRowAtIndexPath!(tableView, indexPath)
}
Expand Down