Skip to content

Commit 1199af0

Browse files
committed
Modified code so toggleSection: call does not require a headerView to exist (be visible). Modified tests to support new changes.
1 parent 60a0bd3 commit 1199af0

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed

FZAccordionTableView/FZAccordionTableView.m

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ - (BOOL)isSectionOpen:(NSInteger)section {
199199

200200
- (void)toggleSection:(NSInteger)section {
201201
FZAccordionTableViewHeaderView *headerView = (FZAccordionTableViewHeaderView *)[self headerViewForSection:section];
202-
[self tappedHeaderView:headerView];
202+
[self toggleSection:section withHeaderView:headerView];
203203
}
204204

205205
- (NSInteger)sectionForHeaderView:(UITableViewHeaderFooterView *)headerView {
@@ -259,11 +259,46 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
259259
NSParameterAssert(sectionHeaderView);
260260

261261
NSInteger section = [self sectionForHeaderView:sectionHeaderView];
262+
[self toggleSection:section withHeaderView:sectionHeaderView];
263+
}
264+
265+
- (void)closeAllSectionsExcept:(NSInteger)section {
266+
// Get all of the sections that we need to close
267+
NSMutableSet *sectionsToClose = [[NSMutableSet alloc] init];
268+
for (NSInteger i = 0; i < self.numberOfSections; i++) {
269+
FZAccordionTableViewSectionInfo *sectionInfo = self.sectionInfos[i];
270+
if (section != i && sectionInfo.isOpen) {
271+
[sectionsToClose addObject:@(i)];
272+
}
273+
}
262274

275+
// Close the found sections
276+
for (NSNumber *sectionToClose in sectionsToClose) {
277+
278+
// Change animations based off which sections are closed
279+
UITableViewRowAnimation closeAnimation = UITableViewRowAnimationTop;
280+
if (section < sectionToClose.integerValue) {
281+
closeAnimation = UITableViewRowAnimationBottom;
282+
}
283+
if (self.enableAnimationFix) {
284+
if (!self.allowsMultipleSelection &&
285+
(sectionToClose.integerValue == self.sectionInfos.count - 1 ||
286+
sectionToClose.integerValue == self.sectionInfos.count - 2)) {
287+
closeAnimation = UITableViewRowAnimationFade;
288+
}
289+
}
290+
291+
[self closeSection:sectionToClose.integerValue withHeaderView:(FZAccordionTableViewHeaderView *)[self headerViewForSection:sectionToClose.integerValue] rowAnimation:closeAnimation];
292+
}
293+
}
294+
295+
#pragma mark - Open / Closing
296+
297+
- (void)toggleSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView {
263298
if (![self canInteractWithHeaderAtSection:section]) {
264299
return;
265300
}
266-
301+
267302
// Keep at least one section open
268303
if (self.keepOneSectionOpen) {
269304
NSInteger countOfOpenSections = 0;
@@ -273,7 +308,7 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
273308
countOfOpenSections++;
274309
}
275310
}
276-
311+
277312
if (countOfOpenSections == 1 && [self isSectionOpen:section]) {
278313
return;
279314
}
@@ -299,39 +334,7 @@ - (void)tappedHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
299334
[self endUpdates];
300335
}
301336

302-
- (void)closeAllSectionsExcept:(NSInteger)section {
303-
// Get all of the sections that we need to close
304-
NSMutableSet *sectionsToClose = [[NSMutableSet alloc] init];
305-
for (NSInteger i = 0; i < self.numberOfSections; i++) {
306-
FZAccordionTableViewSectionInfo *sectionInfo = self.sectionInfos[i];
307-
if (section != i && sectionInfo.isOpen) {
308-
[sectionsToClose addObject:@(i)];
309-
}
310-
}
311-
312-
// Close the found sections
313-
for (NSNumber *sectionToClose in sectionsToClose) {
314-
315-
// Change animations based off which sections are closed
316-
UITableViewRowAnimation closeAnimation = UITableViewRowAnimationTop;
317-
if (section < sectionToClose.integerValue) {
318-
closeAnimation = UITableViewRowAnimationBottom;
319-
}
320-
if (self.enableAnimationFix) {
321-
if (!self.allowsMultipleSelection &&
322-
(sectionToClose.integerValue == self.sectionInfos.count - 1 ||
323-
sectionToClose.integerValue == self.sectionInfos.count - 2)) {
324-
closeAnimation = UITableViewRowAnimationFade;
325-
}
326-
}
327-
328-
[self closeSection:sectionToClose.integerValue withHeaderView:(FZAccordionTableViewHeaderView *)[self headerViewForSection:sectionToClose.integerValue] rowAnimation:closeAnimation];
329-
}
330-
}
331-
332-
#pragma mark - Open / Closing
333-
334-
- (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
337+
- (void)openSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView {
335338
if (![self canInteractWithHeaderAtSection:section]) {
336339
return;
337340
}
@@ -370,11 +373,11 @@ - (void)openSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeade
370373
[self endUpdates];
371374
}
372375

373-
- (void)closeSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView {
376+
- (void)closeSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView {
374377
[self closeSection:section withHeaderView:sectionHeaderView rowAnimation:UITableViewRowAnimationTop];
375378
}
376379

377-
- (void)closeSection:(NSInteger)section withHeaderView:(FZAccordionTableViewHeaderView *)sectionHeaderView rowAnimation:(UITableViewRowAnimation)rowAnimation {
380+
- (void)closeSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView rowAnimation:(UITableViewRowAnimation)rowAnimation {
378381
if (![self canInteractWithHeaderAtSection:section]) {
379382
return;
380383
}

Tests/FZAccordionTableViewUnitTests/FZAccordionTableViewDelegateTests.m

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,23 @@ - (void)testClosingInteraction
122122

123123
#pragma mark - <FZAccordionTableViewDelegate> -
124124

125-
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView willOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header {
125+
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView willOpenSection:(NSInteger)section withHeader:(nullable UITableViewHeaderFooterView *)header {
126126
XCTAssertNotNil(tableView);
127-
XCTAssertNotNil(header);
128127
self.willOpenSectionCalled = YES;
129128
}
130129

131-
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView didOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header {
130+
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView didOpenSection:(NSInteger)section withHeader:(nullable UITableViewHeaderFooterView *)header {
132131
XCTAssertNotNil(tableView);
133-
XCTAssertNotNil(header);
134132
self.didOpenSectionCalled = YES;
135133
}
136134

137-
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView willCloseSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header {
135+
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView willCloseSection:(NSInteger)section withHeader:(nullable UITableViewHeaderFooterView *)header {
138136
XCTAssertNotNil(tableView);
139-
XCTAssertNotNil(header);
140137
self.willCloseSectionCalled = YES;
141138
}
142139

143-
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView didCloseSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header {
140+
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView didCloseSection:(NSInteger)section withHeader:(nullable UITableViewHeaderFooterView *)header {
144141
XCTAssertNotNil(tableView);
145-
XCTAssertNotNil(header);
146142
self.didCloseSectionCalled = YES;
147143
}
148144

Tests/FZAccordionTableViewUnitTests/FZAccordionTableViewGeneralTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ - (void)testSectionToggling {
5454
self.tableView.allowMultipleSectionsOpen = YES;
5555
self.tableView.keepOneSectionOpen = NO;
5656

57+
// Make sure UITableView methods get called / loaded
58+
[self waitForHeaderViewInSection:0];
59+
5760
// First, open all of the sections
5861
for (NSInteger i = 0; i < [self.tableView numberOfSections]; i++) {
59-
[self waitForHeaderViewInSection:i];
6062
[self.tableView toggleSection:i];
61-
6263
XCTAssert([self.tableView isSectionOpen:i], @"Section %d should be open.", (int)i);
6364
}
6465

6566
// Second, close all of the section
6667
for (NSInteger i = 0; i < [self.tableView numberOfSections]; i++) {
67-
[self waitForHeaderViewInSection:i];
6868
[self.tableView toggleSection:i];
6969

7070
XCTAssert(![self.tableView isSectionOpen:i], @"Section %d should be closed.", (int)i);

0 commit comments

Comments
 (0)