@@ -60,18 +60,15 @@ open class CollapseTableView: UITableView {
60
60
61
61
// MARK: Public methods
62
62
63
- public func toggleSection( _ sectionIndex: Int , sectionView : UIView , animated: Bool ) {
63
+ public func toggleSection( _ sectionIndex: Int , animated: Bool ) {
64
64
if sectionIndex >= sectionStates. count {
65
65
return
66
66
}
67
67
let sectionIsOpen = sectionStates [ sectionIndex]
68
- let collapseSectionHeader = sectionView as? CollapseSectionHeader
69
68
if sectionIsOpen {
70
- collapseSectionHeader? . handleChangeToCloseState ( )
71
69
closeSection ( sectionIndex, animated: animated)
72
70
didTapSectionHeaderView ? ( sectionIndex, false )
73
71
} else {
74
- collapseSectionHeader? . handleChangeToOpenState ( )
75
72
openSection ( sectionIndex, animated: animated)
76
73
didTapSectionHeaderView ? ( sectionIndex, true )
77
74
}
@@ -82,6 +79,9 @@ open class CollapseTableView: UITableView {
82
79
return
83
80
}
84
81
setSectionAtIndex ( sectionIndex, open: true )
82
+ if let headerView = headerView ( forSection: sectionIndex) as? CollapseSectionHeader {
83
+ headerView. updateViewForOpenState ( animated: true )
84
+ }
85
85
if animated {
86
86
if let indexPathsToInsert = indexPathsForRowsInSectionAtIndex ( sectionIndex) {
87
87
insertRows ( at: indexPathsToInsert, with: . top)
@@ -92,7 +92,13 @@ open class CollapseTableView: UITableView {
92
92
}
93
93
94
94
public func closeSection( _ sectionIndex: Int , animated: Bool ) {
95
+ if sectionIndex >= sectionStates. count {
96
+ return
97
+ }
95
98
setSectionAtIndex ( sectionIndex, open: false )
99
+ if let headerView = headerView ( forSection: sectionIndex) as? CollapseSectionHeader {
100
+ headerView. updateViewForCloseState ( animated: true )
101
+ }
96
102
if animated {
97
103
if let indexPathsToDelete = indexPathsForRowsInSectionAtIndex ( sectionIndex) {
98
104
deleteRows ( at: indexPathsToDelete, with: . top)
@@ -134,7 +140,7 @@ open class CollapseTableView: UITableView {
134
140
guard let view = sender. view, view. tag >= 0 else {
135
141
return
136
142
}
137
- toggleSection ( view. tag, sectionView : view , animated: true )
143
+ toggleSection ( view. tag, animated: true )
138
144
}
139
145
}
140
146
@@ -181,10 +187,17 @@ extension CollapseTableView: UITableViewDelegate {
181
187
}
182
188
}
183
189
if !tapGestureFound {
184
- view. tag = section
185
190
view. addGestureRecognizer ( UITapGestureRecognizer ( target: self , action: #selector( handleTapGesture ( _: ) ) ) )
186
191
}
187
192
}
193
+ view. tag = section
194
+ if let collapseSectionHeader = view as? CollapseSectionHeader {
195
+ if isOpenSection ( section) {
196
+ collapseSectionHeader. updateViewForOpenState ( animated: false )
197
+ } else {
198
+ collapseSectionHeader. updateViewForCloseState ( animated: false )
199
+ }
200
+ }
188
201
return view
189
202
}
190
203
}
0 commit comments