From a4ebd6a10a7a1268223fe1b029579927b2d1583e Mon Sep 17 00:00:00 2001 From: Jonathan Gomes Date: Wed, 24 Jul 2019 12:00:16 -0300 Subject: [PATCH] add cell delegate to handle after events for cell expansion and collapse --- ExpandableCell.xcodeproj/project.pbxproj | 4 ++++ ExpandableCell/ExpandableCell.swift | 6 +++++- ExpandableCell/ExpandableCellDelegate.swift | 18 ++++++++++++++++++ ExpandableCellDemo/ViewController.swift | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 ExpandableCell/ExpandableCellDelegate.swift diff --git a/ExpandableCell.xcodeproj/project.pbxproj b/ExpandableCell.xcodeproj/project.pbxproj index 5329a32..fa2d524 100644 --- a/ExpandableCell.xcodeproj/project.pbxproj +++ b/ExpandableCell.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 19EF95001F37580B00085BCA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 19EF94FE1F37580B00085BCA /* Main.storyboard */; }; 19EF95021F37580B00085BCA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 19EF95011F37580B00085BCA /* Assets.xcassets */; }; 19EF950B1F3758C300085BCA /* ExpandableTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19EF950A1F3758C300085BCA /* ExpandableTableView.swift */; }; + AB3919EF22E89D080041AD58 /* ExpandableCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB3919EE22E89D080041AD58 /* ExpandableCellDelegate.swift */; }; BF790BC021A3E5DF00BF5B66 /* InitiallyExpandedExpandableCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BF790BBE21A3E5DF00BF5B66 /* InitiallyExpandedExpandableCell.xib */; }; BF9048D521776DE100BE514F /* ExpandableCell.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 19EF94E91F3757AC00085BCA /* ExpandableCell.framework */; }; BF9048DF21776E1400BE514F /* MemoryLeakTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF9048DE21776E1400BE514F /* MemoryLeakTest.swift */; }; @@ -90,6 +91,7 @@ 19EF95011F37580B00085BCA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 19EF95061F37580B00085BCA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 19EF950A1F3758C300085BCA /* ExpandableTableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpandableTableView.swift; sourceTree = ""; }; + AB3919EE22E89D080041AD58 /* ExpandableCellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandableCellDelegate.swift; sourceTree = ""; }; BF790BBE21A3E5DF00BF5B66 /* InitiallyExpandedExpandableCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = InitiallyExpandedExpandableCell.xib; sourceTree = ""; }; BF9048D021776DE100BE514F /* ExpandableCellTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ExpandableCellTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; BF9048D421776DE100BE514F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -169,6 +171,7 @@ 19E5B2661F39DD9400E7D311 /* ExpandableProcessor.swift */, 19EF94EC1F3757AC00085BCA /* ExpandableCell.h */, 19EF94ED1F3757AC00085BCA /* Info.plist */, + AB3919EE22E89D080041AD58 /* ExpandableCellDelegate.swift */, ); path = ExpandableCell; sourceTree = ""; @@ -355,6 +358,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + AB3919EF22E89D080041AD58 /* ExpandableCellDelegate.swift in Sources */, 19E5B2671F39DD9400E7D311 /* ExpandableProcessor.swift in Sources */, 1921BF6E1F37FF2700BFD528 /* ExpandableDelegate.swift in Sources */, 196801921F3EB97B002DFC73 /* ExpandableStyle.swift in Sources */, diff --git a/ExpandableCell/ExpandableCell.swift b/ExpandableCell/ExpandableCell.swift index f8a52e1..8fbeb63 100644 --- a/ExpandableCell/ExpandableCell.swift +++ b/ExpandableCell/ExpandableCell.swift @@ -14,7 +14,9 @@ open class ExpandableCell: UITableViewCell { open var highlightAnimation = HighlightAnimation.animated private var isOpen = false private var initialExpansionAllowed = true - + + public var expandableDelegate: ExpandableCellDelegate? + public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) @@ -49,6 +51,7 @@ open class ExpandableCell: UITableViewCell { func open() { self.isOpen = true self.initialExpansionAllowed = false + self.expandableDelegate?.expandableCell(expandedCell: self) if highlightAnimation == .animated { UIView.animate(withDuration: 0.3) {[weak self] in self?.arrowImageView.layer.transform = CATransform3DMakeRotation(CGFloat(Double.pi), 1.0, 0.0, 0.0) @@ -58,6 +61,7 @@ open class ExpandableCell: UITableViewCell { func close() { self.isOpen = false + self.expandableDelegate?.expandableCell(collapsedCell: self) if highlightAnimation == .animated { UIView.animate(withDuration: 0.3) {[weak self] in self?.arrowImageView.layer.transform = CATransform3DMakeRotation(CGFloat(Double.pi), 0.0, 0.0, 0.0) diff --git a/ExpandableCell/ExpandableCellDelegate.swift b/ExpandableCell/ExpandableCellDelegate.swift new file mode 100644 index 0000000..45a17de --- /dev/null +++ b/ExpandableCell/ExpandableCellDelegate.swift @@ -0,0 +1,18 @@ +// +// ExpandableCellDelegate.swift +// ExpandableCell +// +// Created by Jonathan Gomes on 24/07/19. +// Copyright © 2019 SeungyounYi. All rights reserved. +// + +import Foundation + +import UIKit + +public protocol ExpandableCellDelegate { + + func expandableCell(expandedCell expandableCell: ExpandableCell) + func expandableCell(collapsedCell expandableCell: ExpandableCell) + +} diff --git a/ExpandableCellDemo/ViewController.swift b/ExpandableCellDemo/ViewController.swift index c8d0d98..4eac749 100644 --- a/ExpandableCellDemo/ViewController.swift +++ b/ExpandableCellDemo/ViewController.swift @@ -203,6 +203,10 @@ extension ViewController: ExpandableDelegate { func expandableTableView(_ expandableTableView: ExpandableTableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = expandableTableView.dequeueReusableCell(withIdentifier: parentCells[indexPath.section][indexPath.row]) else { return UITableViewCell() } + if let cellExp = cell as? ExpandableCell { + cellExp.expandableDelegate = self + return cellExp + } return cell } @@ -261,3 +265,15 @@ extension ViewController: ExpandableDelegate { // return 33 // } } +extension ViewController: ExpandableCellDelegate { + func expandableCell(expandedCell expandableCell: ExpandableCell) { + print("expandedCell") + } + + func expandableCell(collapsedCell expandableCell: ExpandableCell) { + print("collapsedCell") + } + + +} +