Skip to content

Commit 935d59f

Browse files
committed
Fixed a bug where adding a section while others are open would cause a crash and wrote a test for it.
1 parent c843ac4 commit 935d59f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

FZAccordionTableView/FZAccordionTableView.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ - (void)setDataSource:(id<UITableViewDataSource>)dataSource {
150150
super.dataSource = self;
151151
}
152152

153+
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
154+
[sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL * _Nonnull stop) {
155+
FZAccordionTableViewSectionInfo *sectionInfo = [[FZAccordionTableViewSectionInfo alloc] initWithNumberOfRows:0];
156+
[self.sectionInfos insertObject:sectionInfo atIndex:section];
157+
}];
158+
159+
[super insertSections:sections withRowAnimation:animation];
160+
}
161+
153162
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
154163

155164
// Remove section info in reverse order to prevent array from
@@ -431,7 +440,11 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
431440

432441
[self.sectionInfos[section] setNumberOfRows:numOfRows];
433442

434-
return ([self isSectionOpen:section]) ? numOfRows : 0;
443+
if (![self isSectionOpen:section]) {
444+
numOfRows = 0;
445+
}
446+
447+
return numOfRows;
435448
}
436449

437450
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Tests/FZAccordionTableViewUnitTests/FZAccordionTableViewGeneralTests.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ - (void)testAddingSection {
203203
XCTAssert(initialSectionCount+1 == self.tableView.numberOfSections, @"The section counts should match up.");
204204
}
205205

206+
- (void)testAddingSectionWhileOtherSectionsAreOpen {
207+
208+
// First, open all of the sections
209+
self.tableView.allowMultipleSectionsOpen = YES;
210+
for (NSInteger i = 0; i < [self.tableView numberOfSections]; i++) {
211+
[self waitForHeaderViewInSection:i];
212+
[self.tableView toggleSection:i];
213+
214+
XCTAssert([self.tableView isSectionOpen:i], @"Section %d should be open.", (int)i);
215+
}
216+
217+
NSInteger section = 1;
218+
NSInteger numberOfRows = 33;
219+
NSInteger initialSectionCount = self.mainViewController.sections.count;
220+
221+
// Add data into data source
222+
[self.mainViewController.sections insertObject:@(numberOfRows) atIndex:section];
223+
224+
// Add to tableview
225+
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
226+
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
227+
228+
XCTAssert([self.mainViewController.sections[section] integerValue] == [self.tableView.sectionInfos[section] numberOfRows], @"The data source section rows should match up.");
229+
XCTAssert(initialSectionCount+1 == self.tableView.numberOfSections, @"The section counts should match up.");
230+
}
231+
206232
#pragma mark - Delete Section -
207233

208234
- (void)testDeleteClosedSection {

0 commit comments

Comments
 (0)