@@ -89,11 +89,22 @@ - (instancetype)initWithNumberOfRows:(NSInteger)numberOfRows {
89
89
}
90
90
@end
91
91
92
+ #pragma mark -
93
+ #pragma mark - FZAccordionTableViewDelegateProxy
94
+ #pragma mark -
95
+
96
+ @interface FZAccordionTableViewDelegateProxy : NSObject <UITableViewDataSource, UITableViewDelegate>
97
+ @property (nonatomic , weak , readonly ) FZAccordionTableView *accordionTableView;
98
+ - (instancetype )initWithAccordionTableView : (FZAccordionTableView *)accordionTableView ;
99
+ @end
100
+
92
101
#pragma mark -
93
102
#pragma mark - FZAccordionTableView
94
103
#pragma mark -
95
104
96
- @interface FZAccordionTableView () <UITableViewDataSource, UITableViewDelegate, FZAccordionTableViewHeaderViewDelegate>
105
+ @interface FZAccordionTableView () <FZAccordionTableViewHeaderViewDelegate>
106
+
107
+ @property (nonatomic , strong ) FZAccordionTableViewDelegateProxy *delegateProxy;
97
108
98
109
@property (weak , nonatomic ) id <UITableViewDelegate, FZAccordionTableViewDelegate> subclassDelegate;
99
110
@property (weak , nonatomic ) id <UITableViewDataSource> subclassDataSource;
@@ -128,6 +139,7 @@ - (void)initializeVars {
128
139
_allowMultipleSectionsOpen = NO ;
129
140
_enableAnimationFix = NO ;
130
141
_keepOneSectionOpen = NO ;
142
+ _delegateProxy = [[FZAccordionTableViewDelegateProxy alloc ] initWithAccordionTableView: self ];
131
143
}
132
144
133
145
#pragma mark - Override Setters -
@@ -142,12 +154,12 @@ - (void)setInitialOpenSections:(NSSet *)initialOpenedSections {
142
154
143
155
- (void )setDelegate : (id <UITableViewDelegate, FZAccordionTableViewDelegate>)delegate {
144
156
self.subclassDelegate = delegate;
145
- super.delegate = self;
157
+ super.delegate = self. delegateProxy ;
146
158
}
147
159
148
160
- (void )setDataSource : (id <UITableViewDataSource>)dataSource {
149
161
self.subclassDataSource = dataSource;
150
- super.dataSource = self;
162
+ super.dataSource = self. delegateProxy ;
151
163
}
152
164
153
165
- (void )insertSections : (NSIndexSet *)sections withRowAnimation : (UITableViewRowAnimation)animation {
@@ -179,23 +191,6 @@ - (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnima
179
191
[super insertRowsAtIndexPaths: indexPaths withRowAnimation: animation];
180
192
}
181
193
182
- #pragma mark - Forwarding handling -
183
-
184
- - (id )forwardingTargetForSelector : (SEL )aSelector {
185
- if ([self .subclassDataSource respondsToSelector: aSelector]) {
186
- return self.subclassDataSource ;
187
- }
188
- else if ([self .subclassDelegate respondsToSelector: aSelector]) {
189
- return self.subclassDelegate ;
190
- }
191
-
192
- return [super forwardingTargetForSelector: aSelector];
193
- }
194
-
195
- - (BOOL )respondsToSelector : (SEL )aSelector {
196
- return [super respondsToSelector: aSelector] || [self .subclassDelegate respondsToSelector: aSelector] || [self .subclassDataSource respondsToSelector: aSelector];
197
- }
198
-
199
194
#pragma mark - Public Helper Methods -
200
195
201
196
- (BOOL )isSectionOpen : (NSInteger )section {
@@ -399,50 +394,83 @@ - (void)closeSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHead
399
394
[self endUpdates ];
400
395
}
401
396
397
+ @end
398
+
399
+ #pragma mark -
400
+ #pragma mark - FZAccordionTableViewDelegateProxy
401
+ #pragma mark -
402
+
403
+ @implementation FZAccordionTableViewDelegateProxy
404
+
405
+ - (instancetype )initWithAccordionTableView : (FZAccordionTableView *)accordionTableView
406
+ {
407
+ if (self = [super init ]) {
408
+ _accordionTableView = accordionTableView;
409
+ }
410
+ return self;
411
+ }
412
+
413
+ #pragma mark - Forwarding handling -
414
+
415
+ - (id )forwardingTargetForSelector : (SEL )aSelector {
416
+ if ([self .accordionTableView.subclassDataSource respondsToSelector: aSelector]) {
417
+ return self.accordionTableView .subclassDataSource ;
418
+ }
419
+ else if ([self .accordionTableView.subclassDelegate respondsToSelector: aSelector]) {
420
+ return self.accordionTableView .subclassDelegate ;
421
+ }
422
+
423
+ return [super forwardingTargetForSelector: aSelector];
424
+ }
425
+
426
+ - (BOOL )respondsToSelector : (SEL )aSelector {
427
+ return [super respondsToSelector: aSelector] || [self .accordionTableView.subclassDelegate respondsToSelector: aSelector] || [self .accordionTableView.subclassDataSource respondsToSelector: aSelector];
428
+ }
429
+
402
430
#pragma mark - <UITableViewDataSource> -
403
431
404
432
- (NSInteger )numberOfSectionsInTableView : (UITableView *)tableView {
405
- self.numberOfSectionsCalled = YES ;
433
+ self.accordionTableView . numberOfSectionsCalled = YES ;
406
434
407
435
NSInteger numOfSections = 1 ; // Default value for UITableView is 1
408
436
409
- if ([self .subclassDataSource respondsToSelector: @selector (numberOfSectionsInTableView: )]) {
410
- numOfSections = [self .subclassDataSource numberOfSectionsInTableView: tableView];
437
+ if ([self .accordionTableView. subclassDataSource respondsToSelector: @selector (numberOfSectionsInTableView: )]) {
438
+ numOfSections = [self .accordionTableView. subclassDataSource numberOfSectionsInTableView: tableView];
411
439
}
412
440
413
441
// Create 'FZAccordionTableViewSectionInfo' objects to represent each section
414
- for (NSInteger i = self.sectionInfos .count ; i < numOfSections; i++) {
442
+ for (NSInteger i = self.accordionTableView . sectionInfos .count ; i < numOfSections; i++) {
415
443
FZAccordionTableViewSectionInfo *section = [[FZAccordionTableViewSectionInfo alloc ] initWithNumberOfRows: 0 ];
416
444
417
445
// Account for any initial open sections
418
- if (self.mutableInitialOpenSections .count > 0 && [self .mutableInitialOpenSections containsObject: @(i)]) {
446
+ if (self.accordionTableView . mutableInitialOpenSections .count > 0 && [self .accordionTableView .mutableInitialOpenSections containsObject: @(i)]) {
419
447
section.open = YES ;
420
- [self .mutableInitialOpenSections removeObject: @(i)];
448
+ [self .accordionTableView. mutableInitialOpenSections removeObject: @(i)];
421
449
}
422
450
423
- [self .sectionInfos addObject: section];
451
+ [self .accordionTableView. sectionInfos addObject: section];
424
452
}
425
453
426
454
return numOfSections;
427
455
}
428
456
429
457
- (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
430
- if (!self.numberOfSectionsCalled ) {
458
+ if (!self.accordionTableView . numberOfSectionsCalled ) {
431
459
// There is some potential UITableView bug where
432
460
// 'tableView:numberOfRowsInSection:' gets called before
433
461
// 'numberOfSectionsInTableView' gets called.
434
462
return 0 ;
435
463
}
436
-
464
+
437
465
NSInteger numOfRows = 0 ;
438
466
439
- if ([self .subclassDataSource respondsToSelector: @selector (tableView:numberOfRowsInSection: )]) {
440
- numOfRows = [self .subclassDataSource tableView: tableView numberOfRowsInSection: section];;
467
+ if ([self .accordionTableView. subclassDataSource respondsToSelector: @selector (tableView:numberOfRowsInSection: )]) {
468
+ numOfRows = [self .accordionTableView. subclassDataSource tableView: tableView numberOfRowsInSection: section];;
441
469
}
442
470
443
- [self .sectionInfos[section] setNumberOfRows: numOfRows];
471
+ [self .accordionTableView. sectionInfos[section] setNumberOfRows: numOfRows];
444
472
445
- if (![self isSectionOpen: section]) {
473
+ if (![self .accordionTableView isSectionOpen: section]) {
446
474
numOfRows = 0 ;
447
475
}
448
476
@@ -451,19 +479,19 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
451
479
452
480
- (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
453
481
// We implement this purely to satisfy the Xcode UITableViewDataSource warning
454
- return [self .subclassDataSource tableView: tableView cellForRowAtIndexPath: indexPath];
482
+ return [self .accordionTableView. subclassDataSource tableView: tableView cellForRowAtIndexPath: indexPath];
455
483
}
456
-
484
+
457
485
#pragma mark - <UITableViewDelegate> -
458
486
459
487
- (UIView*)tableView : (UITableView*)tableView viewForHeaderInSection : (NSInteger )section {
460
488
461
489
FZAccordionTableViewHeaderView *headerView = nil ;
462
490
463
- if ([self .subclassDelegate respondsToSelector: @selector (tableView:viewForHeaderInSection: )]) {
464
- headerView = (FZAccordionTableViewHeaderView *)[self .subclassDelegate tableView: tableView viewForHeaderInSection: section];
491
+ if ([self .accordionTableView. subclassDelegate respondsToSelector: @selector (tableView:viewForHeaderInSection: )]) {
492
+ headerView = (FZAccordionTableViewHeaderView *)[self .accordionTableView. subclassDelegate tableView: tableView viewForHeaderInSection: section];
465
493
if ([headerView isKindOfClass: [FZAccordionTableViewHeaderView class ]]) {
466
- headerView.delegate = self;
494
+ headerView.delegate = self. accordionTableView ;
467
495
}
468
496
}
469
497
0 commit comments