@@ -89,11 +89,22 @@ - (instancetype)initWithNumberOfRows:(NSInteger)numberOfRows {
8989}
9090@end
9191
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+
92101#pragma mark -
93102#pragma mark - FZAccordionTableView
94103#pragma mark -
95104
96- @interface FZAccordionTableView () <UITableViewDataSource, UITableViewDelegate, FZAccordionTableViewHeaderViewDelegate>
105+ @interface FZAccordionTableView () <FZAccordionTableViewHeaderViewDelegate>
106+
107+ @property (nonatomic , strong ) FZAccordionTableViewDelegateProxy *delegateProxy;
97108
98109@property (weak , nonatomic ) id <UITableViewDelegate, FZAccordionTableViewDelegate> subclassDelegate;
99110@property (weak , nonatomic ) id <UITableViewDataSource> subclassDataSource;
@@ -128,6 +139,7 @@ - (void)initializeVars {
128139 _allowMultipleSectionsOpen = NO ;
129140 _enableAnimationFix = NO ;
130141 _keepOneSectionOpen = NO ;
142+ _delegateProxy = [[FZAccordionTableViewDelegateProxy alloc ] initWithAccordionTableView: self ];
131143}
132144
133145#pragma mark - Override Setters -
@@ -142,12 +154,12 @@ - (void)setInitialOpenSections:(NSSet *)initialOpenedSections {
142154
143155- (void )setDelegate : (id <UITableViewDelegate, FZAccordionTableViewDelegate>)delegate {
144156 self.subclassDelegate = delegate;
145- super.delegate = self;
157+ super.delegate = self. delegateProxy ;
146158}
147159
148160- (void )setDataSource : (id <UITableViewDataSource>)dataSource {
149161 self.subclassDataSource = dataSource;
150- super.dataSource = self;
162+ super.dataSource = self. delegateProxy ;
151163}
152164
153165- (void )insertSections : (NSIndexSet *)sections withRowAnimation : (UITableViewRowAnimation)animation {
@@ -179,23 +191,6 @@ - (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnima
179191 [super insertRowsAtIndexPaths: indexPaths withRowAnimation: animation];
180192}
181193
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-
199194#pragma mark - Public Helper Methods -
200195
201196- (BOOL )isSectionOpen : (NSInteger )section {
@@ -399,50 +394,83 @@ - (void)closeSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHead
399394 [self endUpdates ];
400395}
401396
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+
402430#pragma mark - <UITableViewDataSource> -
403431
404432- (NSInteger )numberOfSectionsInTableView : (UITableView *)tableView {
405- self.numberOfSectionsCalled = YES ;
433+ self.accordionTableView . numberOfSectionsCalled = YES ;
406434
407435 NSInteger numOfSections = 1 ; // Default value for UITableView is 1
408436
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];
411439 }
412440
413441 // 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++) {
415443 FZAccordionTableViewSectionInfo *section = [[FZAccordionTableViewSectionInfo alloc ] initWithNumberOfRows: 0 ];
416444
417445 // 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)]) {
419447 section.open = YES ;
420- [self .mutableInitialOpenSections removeObject: @(i)];
448+ [self .accordionTableView. mutableInitialOpenSections removeObject: @(i)];
421449 }
422450
423- [self .sectionInfos addObject: section];
451+ [self .accordionTableView. sectionInfos addObject: section];
424452 }
425453
426454 return numOfSections;
427455}
428456
429457- (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
430- if (!self.numberOfSectionsCalled ) {
458+ if (!self.accordionTableView . numberOfSectionsCalled ) {
431459 // There is some potential UITableView bug where
432460 // 'tableView:numberOfRowsInSection:' gets called before
433461 // 'numberOfSectionsInTableView' gets called.
434462 return 0 ;
435463 }
436-
464+
437465 NSInteger numOfRows = 0 ;
438466
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];;
441469 }
442470
443- [self .sectionInfos[section] setNumberOfRows: numOfRows];
471+ [self .accordionTableView. sectionInfos[section] setNumberOfRows: numOfRows];
444472
445- if (![self isSectionOpen: section]) {
473+ if (![self .accordionTableView isSectionOpen: section]) {
446474 numOfRows = 0 ;
447475 }
448476
@@ -451,19 +479,19 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
451479
452480- (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
453481 // 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];
455483}
456-
484+
457485#pragma mark - <UITableViewDelegate> -
458486
459487- (UIView*)tableView : (UITableView*)tableView viewForHeaderInSection : (NSInteger )section {
460488
461489 FZAccordionTableViewHeaderView *headerView = nil ;
462490
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];
465493 if ([headerView isKindOfClass: [FZAccordionTableViewHeaderView class ]]) {
466- headerView.delegate = self;
494+ headerView.delegate = self. accordionTableView ;
467495 }
468496 }
469497
0 commit comments