@@ -136,10 +136,6 @@ - (BOOL)isSectionOpen:(NSInteger)section {
136136 return [self .sectionInfos[section] isOpen ];
137137}
138138
139- - (BOOL )isAlwaysOpenedSection : (NSInteger )section {
140- return [self .sectionsAlwaysOpen containsObject: @(section)];
141- }
142-
143139- (void )markSection : (NSInteger )section open : (BOOL )open {
144140 [self .sectionInfos[section] setOpen: open];
145141}
@@ -186,6 +182,14 @@ - (NSInteger)sectionForHeaderView:(UITableViewHeaderFooterView *)headerView {
186182 return section;
187183}
188184
185+ - (BOOL )canInteractWithHeaderAtSection : (NSInteger )section {
186+ BOOL canInteractWithHeader = YES ;
187+ if ([self .delegate respondsToSelector: @selector (tableView:canInteractWithHeaderAtSection: )]) {
188+ canInteractWithHeader = [self .subclassDelegate tableView: self canInteractWithHeaderAtSection: section];
189+ }
190+ return canInteractWithHeader;
191+ }
192+
189193#pragma mark - UITableView Overrides -
190194
191195- (void )setDelegate : (id <UITableViewDelegate, FZAccordionTableViewDelegate>)delegate {
@@ -207,18 +211,6 @@ - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAn
207211 }];
208212
209213 [super deleteSections: sections withRowAnimation: animation];
210-
211- // Re-open any sections that were closed by deletion, but
212- // must be always open.
213- if (self.sectionsAlwaysOpen .count > 0 ) {
214- [sections enumerateIndexesWithOptions: NSEnumerationReverse usingBlock: ^(NSUInteger section, BOOL * _Nonnull stop) {
215- if ([self .sectionsAlwaysOpen containsObject: @(section)] && ![self isSectionOpen: section]) {
216- FZAccordionTableViewHeaderView *sectionHeaderView = (FZAccordionTableViewHeaderView *)[self headerViewForSection: section];
217- NSArray *indexPathsToModify = [self getIndexPathsForSection: section];
218- [self openSection: section withHeaderView: sectionHeaderView andIndexPaths: indexPathsToModify];
219- }
220- }];
221- }
222214}
223215
224216- (void )insertRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths withRowAnimation : (UITableViewRowAnimation)animation {
@@ -253,23 +245,17 @@ - (void)setInitialOpenSections:(NSSet *)initialOpenedSections {
253245 _mutableInitialOpenSections = [initialOpenedSections mutableCopy ];
254246}
255247
256- - (void )setSectionsAlwaysOpen : (NSSet <NSNumber *> *)sectionsAlwaysOpen {
257- NSAssert (self.sectionInfos.count == 0 , @" 'sectionsAlwaysOpen' MUST be set before the tableView has started loading data." );
258- _sectionsAlwaysOpen = sectionsAlwaysOpen;
259- }
260-
261248#pragma mark - FZAccordionTableViewHeaderViewDelegate -
262249
263250- (void )tappedHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView {
264251 NSParameterAssert (sectionHeaderView);
265252
266253 NSInteger section = [self sectionForHeaderView: sectionHeaderView];
267254
268- // Do not interact with sections that are always opened
269- if ([self isAlwaysOpenedSection: section]) {
255+ if (![self canInteractWithHeaderAtSection: section]) {
270256 return ;
271257 }
272-
258+
273259 // Keep at least one section open
274260 if (self.keepOneSectionOpen ) {
275261 NSInteger countOfOpenSections = 0 ;
@@ -279,31 +265,22 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
279265 countOfOpenSections++;
280266 }
281267 }
282-
283- if (self.sectionsAlwaysOpen .count > 0 ) {
284- // 'sectionsAlwaysOpen' does not have an influence
285- // on 'keepOneSectionOpen'
286- countOfOpenSections -= self.sectionsAlwaysOpen .count ;
287- }
288-
268+
289269 if (countOfOpenSections == 1 && [self isSectionOpen: section]) {
290270 return ;
291271 }
292272 }
293273
294274 BOOL openSection = [self isSectionOpen: section];
295275
296- // Create an array of index paths that will be inserted/removed
297- NSArray *indexPathsToModify = [self getIndexPathsForSection: section];
298-
299276 [self beginUpdates ];
300277
301278 // Insert/remove rows to simulate opening/closing of a header
302279 if (!openSection) {
303- [self openSection: section withHeaderView: sectionHeaderView andIndexPaths: indexPathsToModify ];
280+ [self openSection: section withHeaderView: sectionHeaderView];
304281 }
305282 else { // The section is currently open
306- [self closeSection: section withHeaderView: sectionHeaderView andIndexPaths: indexPathsToModify ];
283+ [self closeSection: section withHeaderView: sectionHeaderView];
307284 }
308285
309286 // Auto-collapse the rest of the opened sections
@@ -314,7 +291,11 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
314291 [self endUpdates ];
315292}
316293
317- - (void )openSection : (NSInteger )section withHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView andIndexPaths : (NSArray *)indexPathsToModify {
294+ - (void )openSection : (NSInteger )section withHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView {
295+ if (![self canInteractWithHeaderAtSection: section]) {
296+ return ;
297+ }
298+
318299 if ([self .subclassDelegate respondsToSelector: @selector (tableView:willOpenSection:withHeader: )]) {
319300 [self .subclassDelegate tableView: self willOpenSection: section withHeader: sectionHeaderView];
320301 }
@@ -337,6 +318,7 @@ - (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeade
337318 }
338319 }
339320
321+ NSArray *indexPathsToModify = [self getIndexPathsForSection: section];
340322 [self markSection: section open: YES ];
341323 [self beginUpdates ];
342324 [CATransaction setCompletionBlock: ^{
@@ -348,11 +330,19 @@ - (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeade
348330 [self endUpdates ];
349331}
350332
351- - (void )closeSection : (NSInteger )section withHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView andIndexPaths : (NSArray *)indexPathsToModify {
333+ - (void )closeSection : (NSInteger )section withHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView {
334+ [self closeSection: section withHeaderView: sectionHeaderView rowAnimation: UITableViewRowAnimationTop];
335+ }
336+
337+ - (void )closeSection : (NSInteger )section withHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView rowAnimation : (UITableViewRowAnimation)rowAnimation {
338+ if (![self canInteractWithHeaderAtSection: section]) {
339+ return ;
340+ }
341+
352342 if ([self .subclassDelegate respondsToSelector: @selector (tableView:willCloseSection:withHeader: )]) {
353343 [self .subclassDelegate tableView: self willCloseSection: section withHeader: sectionHeaderView];
354344 }
355-
345+ NSArray *indexPathsToModify = [ self getIndexPathsForSection: section];
356346 [self markSection: section open: NO ];
357347 [self beginUpdates ];
358348 [CATransaction setCompletionBlock: ^{
@@ -369,18 +359,14 @@ - (void)autoCollapseAllSectionsExceptSection:(NSInteger)section {
369359 NSMutableSet *sectionsToClose = [[NSMutableSet alloc ] init ];
370360 for (NSInteger i = 0 ; i < self.numberOfSections ; i++) {
371361 FZAccordionTableViewSectionInfo *sectionInfo = self.sectionInfos [i];
372- if (section != i && sectionInfo.isOpen && ![ self isAlwaysOpenedSection: i] ) {
362+ if (section != i && sectionInfo.isOpen ) {
373363 [sectionsToClose addObject: @(i)];
374364 }
375365 }
376366
377367 // Close the found sections
378368 for (NSNumber *sectionToClose in sectionsToClose) {
379-
380- if ([self .subclassDelegate respondsToSelector: @selector (tableView:willCloseSection:withHeader: )]) {
381- [self .subclassDelegate tableView: self willCloseSection: sectionToClose.integerValue withHeader: [self headerViewForSection: sectionToClose.integerValue]];
382- }
383-
369+
384370 // Change animations based off which sections are closed
385371 UITableViewRowAnimation closeAnimation = UITableViewRowAnimationTop;
386372 if (section < sectionToClose.integerValue ) {
@@ -390,22 +376,11 @@ - (void)autoCollapseAllSectionsExceptSection:(NSInteger)section {
390376 if (!self.allowsMultipleSelection &&
391377 (sectionToClose.integerValue == self.sectionInfos .count - 1 ||
392378 sectionToClose.integerValue == self.sectionInfos .count - 2 )) {
393- closeAnimation = UITableViewRowAnimationFade;
394- }
379+ closeAnimation = UITableViewRowAnimationFade;
380+ }
395381 }
396382
397- // Delete the cells for section that is closing
398- NSArray *indexPathsToDelete = [self getIndexPathsForSection: sectionToClose.integerValue];
399- [self markSection: sectionToClose.integerValue open: NO ];
400-
401- [self beginUpdates ];
402- [CATransaction setCompletionBlock: ^{
403- if ([self .subclassDelegate respondsToSelector: @selector (tableView:didCloseSection:withHeader: )]) {
404- [self .subclassDelegate tableView: self didCloseSection: sectionToClose.integerValue withHeader: [self headerViewForSection: sectionToClose.integerValue]];
405- }
406- }];
407- [self deleteRowsAtIndexPaths: indexPathsToDelete withRowAnimation: closeAnimation];
408- [self endUpdates ];
383+ [self closeSection: sectionToClose.integerValue withHeaderView: (FZAccordionTableViewHeaderView *)[self headerViewForSection: sectionToClose.integerValue] rowAnimation: closeAnimation];
409384 }
410385}
411386
@@ -429,10 +404,6 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
429404 section.open = YES ;
430405 [self .mutableInitialOpenSections removeObject: @(i)];
431406 }
432- // Account for sections that are always open
433- else {
434- section.open = [self .sectionsAlwaysOpen containsObject: @(i)];
435- }
436407
437408 [self .sectionInfos addObject: section];
438409 }
0 commit comments