diff --git a/Example/Views/AccordionTableViewController.swift b/Example/Views/AccordionTableViewController.swift index 8d56b80..b50e701 100644 --- a/Example/Views/AccordionTableViewController.swift +++ b/Example/Views/AccordionTableViewController.swift @@ -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 @@ -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 diff --git a/Example/Views/AccordionViewController.swift b/Example/Views/AccordionViewController.swift index 9f217cc..ab24f5a 100644 --- a/Example/Views/AccordionViewController.swift +++ b/Example/Views/AccordionViewController.swift @@ -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 @@ -137,6 +141,7 @@ extension AccordionViewController { childCellConfig: childCellConfig, didSelectParentAtIndexPath: didSelectParentCell, didSelectChildAtIndexPath: didSelectChildCell, + didDeselectChildAtIndexPath: didDeselectChildCell, heightForParentCellAtIndexPath: heightForParentCell, heightForChildCellAtIndexPath: heightForChildCell, scrollViewDidScroll: scrollViewDidScroll diff --git a/Source/DataSourceProvider.swift b/Source/DataSourceProvider.swift index d2b8331..5f4751a 100644 --- a/Source/DataSourceProvider.swift +++ b/Source/DataSourceProvider.swift @@ -24,6 +24,7 @@ public final class DataSourceProvider 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 @@ -59,6 +60,9 @@ public final class DataSourceProvider 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) diff --git a/Source/TableViewDelegate.swift b/Source/TableViewDelegate.swift index ecb0de6..c429697 100644 --- a/Source/TableViewDelegate.swift +++ b/Source/TableViewDelegate.swift @@ -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 @@ -17,6 +18,7 @@ public typealias ScrollViewDidScrollClosure = (UIScrollView) -> Void // MARK: - Properties var didSelectRowAtIndexPath: DidSelectRowAtIndexPathClosure? + var didDeselectRowAtIndexPath: DidDeselectRowAtIntexPathClosure? var heightForRowAtIndexPath: HeightForRowAtIndexPathClosure? var scrollViewDidScrollClosure: ScrollViewDidScrollClosure? @@ -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) }