Skip to content

Commit 0bd0ba8

Browse files
committed
Started implemeneting sectionForHeaderView function for dynamic section finding.
1 parent dca77d6 commit 0bd0ba8

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

Example/Testing_Example/FZAccordionTableViewExample/FirstViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ - (void)viewDidLoad {
3232

3333
- (void)setupData {
3434
self.sections = [NSMutableArray new];
35-
for (NSInteger i = 0; i < 10; i++) {
35+
for (NSInteger i = 0; i < 100; i++) {
3636
[self.sections addObject:@(arc4random_uniform(10))];
3737
}
3838
}

FZAccordionTableView/FZAccordionTableView.m

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ - (void)singleInit {
6767
}
6868

6969
- (void)touchedHeaderView:(UITapGestureRecognizer *)recognizer {
70+
71+
72+
7073
[self.delegate headerView:self didSelectHeaderInSection:self.section];
7174
}
7275

@@ -162,7 +165,7 @@ - (void)toggleSection:(NSInteger)section {
162165
[self headerView:headerView didSelectHeaderInSection:section];
163166
}
164167

165-
#pragma mark - Handle message forwarding -
168+
#pragma mark - UITableView Overrides -
166169

167170
- (void)setDelegate:(id<UITableViewDelegate, FZAccordionTableViewDelegate>)delegate {
168171
self.subclassDelegate = delegate;
@@ -174,6 +177,35 @@ - (void)setDataSource:(id<UITableViewDataSource>)dataSource {
174177
super.dataSource = self;
175178
}
176179

180+
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
181+
182+
// Loop:
183+
// [self headerViewForSection:section];
184+
// stop when its past the possible table view bounds.
185+
// update each of the sections by 1.
186+
187+
188+
// You can also utilize: viewForHeaderInSection
189+
// Save the lowest possible section that was called for viewForHeaderInSection???
190+
//
191+
// [sections enumerateIndexesWithOptions:NSEnumerationReverse usingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
192+
//
193+
// }];
194+
//
195+
// for (int i = sections.firstIndex; i < self.numberOfSections; i++) {
196+
// FZAccordionTableViewHeaderView *headerView = [self tableView:self viewForHeaderInSection:i];
197+
// headerView.section--;
198+
// }
199+
200+
[sections enumerateIndexesUsingBlock:^(NSUInteger index, BOOL * _Nonnull stop) {
201+
self.numOfRowsForSection[@(index)] = nil;
202+
[self.openedSections removeObject:@(index)];
203+
}];
204+
[super deleteSections:sections withRowAnimation:animation];
205+
}
206+
207+
#pragma mark - Forwarding handling -
208+
177209
- (id)forwardingTargetForSelector:(SEL)aSelector {
178210
if ([self.subclassDataSource respondsToSelector:aSelector]) {
179211
return self.subclassDataSource;
@@ -213,8 +245,41 @@ - (void)setInitialOpenSections:(NSSet *)initialOpenedSections {
213245

214246
#pragma mark - FZAccordionTableViewHeaderViewDelegate -
215247

248+
249+
250+
- (NSInteger)sectionForHeaderView:(UITableViewHeaderFooterView *)headerView {
251+
252+
NSInteger section = NSNotFound;
253+
NSInteger min = 0;
254+
NSInteger max = self.numberOfSections;
255+
256+
CGRect headerViewFrame = headerView.frame;
257+
CGRect compareHeaderViewFrame;
258+
259+
while (min <= max) {
260+
NSInteger mid = (min+max)/2;
261+
compareHeaderViewFrame = [self rectForHeaderInSection:mid];
262+
if (CGRectEqualToRect(headerViewFrame, compareHeaderViewFrame)) {
263+
section = mid;
264+
break;
265+
}
266+
else if (headerViewFrame.origin.y > compareHeaderViewFrame.origin.y) {
267+
min = mid+1;
268+
}
269+
else {
270+
max = mid-1;
271+
}
272+
}
273+
274+
return section;
275+
}
276+
277+
278+
216279
- (void)headerView:(FZAccordionTableViewHeaderView *)sectionHeaderView didSelectHeaderInSection:(NSInteger)section {
217280

281+
section = [self sectionForHeaderView:sectionHeaderView];
282+
218283
// Do not interact with sections that are always opened
219284
if ([self isAlwaysOpenedSection:section]) {
220285
return;

0 commit comments

Comments
 (0)