@@ -136,10 +136,6 @@ - (BOOL)isSectionOpen:(NSInteger)section {
136
136
return [self .sectionInfos[section] isOpen ];
137
137
}
138
138
139
- - (BOOL )isAlwaysOpenedSection : (NSInteger )section {
140
- return [self .sectionsAlwaysOpen containsObject: @(section)];
141
- }
142
-
143
139
- (void )markSection : (NSInteger )section open : (BOOL )open {
144
140
[self .sectionInfos[section] setOpen: open];
145
141
}
@@ -186,6 +182,14 @@ - (NSInteger)sectionForHeaderView:(UITableViewHeaderFooterView *)headerView {
186
182
return section;
187
183
}
188
184
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
+
189
193
#pragma mark - UITableView Overrides -
190
194
191
195
- (void )setDelegate : (id <UITableViewDelegate, FZAccordionTableViewDelegate>)delegate {
@@ -207,18 +211,6 @@ - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAn
207
211
}];
208
212
209
213
[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
- }
222
214
}
223
215
224
216
- (void )insertRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths withRowAnimation : (UITableViewRowAnimation)animation {
@@ -253,23 +245,17 @@ - (void)setInitialOpenSections:(NSSet *)initialOpenedSections {
253
245
_mutableInitialOpenSections = [initialOpenedSections mutableCopy ];
254
246
}
255
247
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
-
261
248
#pragma mark - FZAccordionTableViewHeaderViewDelegate -
262
249
263
250
- (void )tappedHeaderView : (FZAccordionTableViewHeaderView *)sectionHeaderView {
264
251
NSParameterAssert (sectionHeaderView);
265
252
266
253
NSInteger section = [self sectionForHeaderView: sectionHeaderView];
267
254
268
- // Do not interact with sections that are always opened
269
- if ([self isAlwaysOpenedSection: section]) {
255
+ if (![self canInteractWithHeaderAtSection: section]) {
270
256
return ;
271
257
}
272
-
258
+
273
259
// Keep at least one section open
274
260
if (self.keepOneSectionOpen ) {
275
261
NSInteger countOfOpenSections = 0 ;
@@ -279,31 +265,22 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
279
265
countOfOpenSections++;
280
266
}
281
267
}
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
+
289
269
if (countOfOpenSections == 1 && [self isSectionOpen: section]) {
290
270
return ;
291
271
}
292
272
}
293
273
294
274
BOOL openSection = [self isSectionOpen: section];
295
275
296
- // Create an array of index paths that will be inserted/removed
297
- NSArray *indexPathsToModify = [self getIndexPathsForSection: section];
298
-
299
276
[self beginUpdates ];
300
277
301
278
// Insert/remove rows to simulate opening/closing of a header
302
279
if (!openSection) {
303
- [self openSection: section withHeaderView: sectionHeaderView andIndexPaths: indexPathsToModify ];
280
+ [self openSection: section withHeaderView: sectionHeaderView];
304
281
}
305
282
else { // The section is currently open
306
- [self closeSection: section withHeaderView: sectionHeaderView andIndexPaths: indexPathsToModify ];
283
+ [self closeSection: section withHeaderView: sectionHeaderView];
307
284
}
308
285
309
286
// Auto-collapse the rest of the opened sections
@@ -314,7 +291,11 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
314
291
[self endUpdates ];
315
292
}
316
293
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
+
318
299
if ([self .subclassDelegate respondsToSelector: @selector (tableView:willOpenSection:withHeader: )]) {
319
300
[self .subclassDelegate tableView: self willOpenSection: section withHeader: sectionHeaderView];
320
301
}
@@ -337,6 +318,7 @@ - (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeade
337
318
}
338
319
}
339
320
321
+ NSArray *indexPathsToModify = [self getIndexPathsForSection: section];
340
322
[self markSection: section open: YES ];
341
323
[self beginUpdates ];
342
324
[CATransaction setCompletionBlock: ^{
@@ -348,11 +330,19 @@ - (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeade
348
330
[self endUpdates ];
349
331
}
350
332
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
+
352
342
if ([self .subclassDelegate respondsToSelector: @selector (tableView:willCloseSection:withHeader: )]) {
353
343
[self .subclassDelegate tableView: self willCloseSection: section withHeader: sectionHeaderView];
354
344
}
355
-
345
+ NSArray *indexPathsToModify = [ self getIndexPathsForSection: section];
356
346
[self markSection: section open: NO ];
357
347
[self beginUpdates ];
358
348
[CATransaction setCompletionBlock: ^{
@@ -369,18 +359,14 @@ - (void)autoCollapseAllSectionsExceptSection:(NSInteger)section {
369
359
NSMutableSet *sectionsToClose = [[NSMutableSet alloc ] init ];
370
360
for (NSInteger i = 0 ; i < self.numberOfSections ; i++) {
371
361
FZAccordionTableViewSectionInfo *sectionInfo = self.sectionInfos [i];
372
- if (section != i && sectionInfo.isOpen && ![ self isAlwaysOpenedSection: i] ) {
362
+ if (section != i && sectionInfo.isOpen ) {
373
363
[sectionsToClose addObject: @(i)];
374
364
}
375
365
}
376
366
377
367
// Close the found sections
378
368
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
+
384
370
// Change animations based off which sections are closed
385
371
UITableViewRowAnimation closeAnimation = UITableViewRowAnimationTop;
386
372
if (section < sectionToClose.integerValue ) {
@@ -390,22 +376,11 @@ - (void)autoCollapseAllSectionsExceptSection:(NSInteger)section {
390
376
if (!self.allowsMultipleSelection &&
391
377
(sectionToClose.integerValue == self.sectionInfos .count - 1 ||
392
378
sectionToClose.integerValue == self.sectionInfos .count - 2 )) {
393
- closeAnimation = UITableViewRowAnimationFade;
394
- }
379
+ closeAnimation = UITableViewRowAnimationFade;
380
+ }
395
381
}
396
382
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];
409
384
}
410
385
}
411
386
@@ -429,10 +404,6 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
429
404
section.open = YES ;
430
405
[self .mutableInitialOpenSections removeObject: @(i)];
431
406
}
432
- // Account for sections that are always open
433
- else {
434
- section.open = [self .sectionsAlwaysOpen containsObject: @(i)];
435
- }
436
407
437
408
[self .sectionInfos addObject: section];
438
409
}
0 commit comments