From 328941e374b7f5f7b63a258070453a9d65e9b29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Jesus?= Date: Wed, 26 Nov 2014 18:00:01 +0000 Subject: [PATCH 1/4] Fixed crash when view was dealocced and delegate was set to nil. --- SLExpandableTableView/SLExpandableTableView.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index e995cd5..8b7d9af 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -44,8 +44,10 @@ @implementation SLExpandableTableView } - (void)setDelegate:(id)delegate { - _myDelegate = delegate; - [super setDelegate:self]; + if (delegate != nil){ + _myDelegate = delegate; + [super setDelegate:self]; + } } - (id)dataSource { From 8ddc5e0c00f9b7810d6338dd7f08719b06c54c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Jesus?= Date: Tue, 10 Feb 2015 11:07:54 +0000 Subject: [PATCH 2/4] Added delegate method to control the animation of a cell after collapsing. --- SLExpandableTableView/SLExpandableTableView.h | 4 ++++ SLExpandableTableView/SLExpandableTableView.m | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/SLExpandableTableView/SLExpandableTableView.h b/SLExpandableTableView/SLExpandableTableView.h index ff27fd7..207af04 100644 --- a/SLExpandableTableView/SLExpandableTableView.h +++ b/SLExpandableTableView/SLExpandableTableView.h @@ -33,6 +33,8 @@ typedef enum { - (BOOL)tableView:(SLExpandableTableView *)tableView needsToDownloadDataForExpandableSection:(NSInteger)section; - (UITableViewCell *)tableView:(SLExpandableTableView *)tableView expandingCellForSection:(NSInteger)section; + + @end @@ -51,6 +53,8 @@ typedef enum { - (void)tableView:(SLExpandableTableView *)tableView willCollapseSection:(NSUInteger)section animated:(BOOL)animated; - (void)tableView:(SLExpandableTableView *)tableView didCollapseSection:(NSUInteger)section animated:(BOOL)animated; +- (BOOL)tableView:(SLExpandableTableView *)tableView shouldAnimateScrollToSection:(NSUInteger)section; + @end diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index ecca257..30f4e30 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -303,9 +303,12 @@ - (void)collapseSection:(NSInteger)section animated:(BOOL)animated { [self.animatingSectionsDictionary removeObjectForKey:@(section)]; - [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] + if (![self.myDelegate respondsToSelector:@selector(tableView:shouldAnimateScrollToSection:)] || [self.myDelegate tableView:self shouldAnimateScrollToSection:section]) { + [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:animated]; + + } void(^completionBlock)(void) = ^{ if ([self respondsToSelector:@selector(scrollViewDidScroll:)]) { From b23137acaf3d155bfe0327892b5c5b8c27dde3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Jesus?= Date: Tue, 10 Feb 2015 11:09:41 +0000 Subject: [PATCH 3/4] Indentation. --- SLExpandableTableView/SLExpandableTableView.h | 2 - SLExpandableTableView/SLExpandableTableView.m | 90 +++++++++---------- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/SLExpandableTableView/SLExpandableTableView.h b/SLExpandableTableView/SLExpandableTableView.h index 207af04..eadd996 100644 --- a/SLExpandableTableView/SLExpandableTableView.h +++ b/SLExpandableTableView/SLExpandableTableView.h @@ -33,8 +33,6 @@ typedef enum { - (BOOL)tableView:(SLExpandableTableView *)tableView needsToDownloadDataForExpandableSection:(NSInteger)section; - (UITableViewCell *)tableView:(SLExpandableTableView *)tableView expandingCellForSection:(NSInteger)section; - - @end diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index 30f4e30..f17834d 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -91,7 +91,7 @@ - (BOOL)respondsToSelector:(SEL)aSelector } else if (protocol_containsSelector(@protocol(UITableViewDelegate), aSelector)) { return [super respondsToSelector:aSelector] || [_myDelegate respondsToSelector:aSelector]; } - + return [super respondsToSelector:aSelector]; } @@ -102,7 +102,7 @@ - (id)forwardingTargetForSelector:(SEL)aSelector } else if (protocol_containsSelector(@protocol(UITableViewDelegate), aSelector)) { return _myDelegate; } - + return [super forwardingTargetForSelector:aSelector]; } @@ -134,10 +134,10 @@ - (void)commonInit { - (void)awakeFromNib { [super awakeFromNib]; - + _storedTableHeaderView = self.tableHeaderView; _storedTableFooterView = self.tableFooterView; - + self.tableHeaderView = self.tableHeaderView; self.tableFooterView = self.tableFooterView; } @@ -167,7 +167,7 @@ - (void)reloadDataAndResetExpansionStates:(BOOL)resetFlag { if (resetFlag) { [self _resetExpansionStates]; } - + if (self.onlyDisplayHeaderAndFooterViewIfTableViewIsNotEmpty) { if ([self numberOfSections] > 0) { if ([super tableFooterView] != self.storedTableFooterView) { @@ -185,7 +185,7 @@ - (void)reloadDataAndResetExpansionStates:(BOOL)resetFlag { [super setTableHeaderView:self.storedTableHeaderView]; } } - + [super reloadData]; } @@ -201,60 +201,60 @@ - (void)expandSection:(NSInteger)section animated:(BOOL)animated { // section is already showing, return return; } - + [self deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] animated:NO]; - + if ([self.myDataSource tableView:self needsToDownloadDataForExpandableSection:section]) { // data is still not ready to be displayed, return [self downloadDataInSection:section]; return; } - + if ([self.myDelegate respondsToSelector:@selector(tableView:willExpandSection:animated:)]) { [self.myDelegate tableView:self willExpandSection:section animated:animated]; } - + self.animatingSectionsDictionary[key] = @YES; - + // remove the download state self.downloadingSectionsDictionary[key] = @NO; - + // update the showing state self.showingSectionsDictionary[key] = @YES; - + NSInteger newRowCount = [self.myDataSource tableView:self numberOfRowsInSection:section]; // now do the animation magic to insert the new cells if (animated && newRowCount <= self.maximumRowCountToStillUseAnimationWhileExpanding) { [self beginUpdates]; - + UITableViewCell *cell = (UITableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]; cell.loading = NO; [cell setExpansionStyle:UIExpansionStyleExpanded animated:YES]; - + NSMutableArray *insertArray = [NSMutableArray array]; for (int i = 1; i < newRowCount; i++) { [insertArray addObject:[NSIndexPath indexPathForRow:i inSection:section] ]; } - + [self insertRowsAtIndexPaths:insertArray withRowAnimation:self.reloadAnimation]; - + [self endUpdates]; } else { [self reloadDataAndResetExpansionStates:NO]; } - + [self.animatingSectionsDictionary removeObjectForKey:@(section)]; - + void(^completionBlock)(void) = ^{ if ([self respondsToSelector:@selector(scrollViewDidScroll:)]) { [self scrollViewDidScroll:self]; } - + if ([self.myDelegate respondsToSelector:@selector(tableView:didExpandSection:animated:)]) { [self.myDelegate tableView:self didExpandSection:section animated:animated]; } }; - + if (animated) { [CATransaction setCompletionBlock:completionBlock]; } else { @@ -268,58 +268,58 @@ - (void)collapseSection:(NSInteger)section animated:(BOOL)animated { // section is not showing, return return; } - + if ([self.myDelegate respondsToSelector:@selector(tableView:willCollapseSection:animated:)]) { [self.myDelegate tableView:self willCollapseSection:section animated:animated]; } - + [self deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] animated:NO]; - + self.animatingSectionsDictionary[key] = @YES; - + // update the showing state self.showingSectionsDictionary[key] = @NO; - + NSInteger newRowCount = [self.myDataSource tableView:self numberOfRowsInSection:section]; // now do the animation magic to delete the new cells if (animated && newRowCount <= self.maximumRowCountToStillUseAnimationWhileExpanding) { [self beginUpdates]; - + UITableViewCell *cell = (UITableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]; cell.loading = NO; [cell setExpansionStyle:UIExpansionStyleCollapsed animated:YES]; - + NSMutableArray *deleteArray = [NSMutableArray array]; for (int i = 1; i < newRowCount; i++) { [deleteArray addObject:[NSIndexPath indexPathForRow:i inSection:section] ]; } - + [self deleteRowsAtIndexPaths:deleteArray withRowAnimation:self.reloadAnimation]; - + [self endUpdates]; } else { [self reloadDataAndResetExpansionStates:NO]; } - + [self.animatingSectionsDictionary removeObjectForKey:@(section)]; - - if (![self.myDelegate respondsToSelector:@selector(tableView:shouldAnimateScrollToSection:)] || [self.myDelegate tableView:self shouldAnimateScrollToSection:section]) { - [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] - atScrollPosition:UITableViewScrollPositionTop - animated:animated]; - - } - + + if (![self.myDelegate respondsToSelector:@selector(tableView:shouldAnimateScrollToSection:)] || [self.myDelegate tableView:self shouldAnimateScrollToSection:section]) { + [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] + atScrollPosition:UITableViewScrollPositionTop + animated:animated]; + + } + void(^completionBlock)(void) = ^{ if ([self respondsToSelector:@selector(scrollViewDidScroll:)]) { [self scrollViewDidScroll:self]; } - + if ([self.myDelegate respondsToSelector:@selector(tableView:didCollapseSection:animated:)]) { [self.myDelegate tableView:self didCollapseSection:section animated:animated]; } }; - + if (animated) { [CATransaction setCompletionBlock:completionBlock]; } else { @@ -349,7 +349,7 @@ - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAn if (nextIndex == NSNotFound) { nextIndex = indexCount; } - + for (NSInteger i = currentIndex + 1; i < nextIndex; i++) { NSUInteger newIndex = i - currentShift; self.expandableSectionsDictionary[@(newIndex)] = @([self.expandableSectionsDictionary[@(i)] boolValue]); @@ -357,11 +357,11 @@ - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAn self.downloadingSectionsDictionary[@(newIndex)] = @([self.downloadingSectionsDictionary[@(i)] boolValue]); self.animatingSectionsDictionary[@(newIndex)] = @([self.animatingSectionsDictionary[@(i)] boolValue]); } - + currentShift++; currentIndex = [sections indexLessThanIndex:currentIndex]; } - + [super deleteSections:sections withRowAnimation:animation]; } @@ -419,7 +419,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger return 0; } self.expandableSectionsDictionary[key] = @YES; - + if ([self.showingSectionsDictionary[key] boolValue]) { return [self.myDataSource tableView:tableView numberOfRowsInSection:section]; } else { From 2f12444d46e9cfa8de6b115a9930fd21e99d4d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Jesus?= Date: Mon, 16 Feb 2015 12:55:40 +0000 Subject: [PATCH 4/4] Now animating on expand. --- SLExpandableTableView/SLExpandableTableView.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index f17834d..2fb5178 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -245,6 +245,11 @@ - (void)expandSection:(NSInteger)section animated:(BOOL)animated { [self.animatingSectionsDictionary removeObjectForKey:@(section)]; +[self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] + atScrollPosition:UITableViewScrollPositionTop + animated:animated]; + + void(^completionBlock)(void) = ^{ if ([self respondsToSelector:@selector(scrollViewDidScroll:)]) { [self scrollViewDidScroll:self];