From 00a121983907c2aca90bd778ed67c3d84059e51b Mon Sep 17 00:00:00 2001 From: Adam Fanslau Date: Fri, 26 Jun 2015 14:56:33 -0400 Subject: [PATCH] Made UIExpandingTableViewCell an optional protocol --- SLExpandableTableView/SLExpandableTableView.h | 4 ++ SLExpandableTableView/SLExpandableTableView.m | 44 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/SLExpandableTableView/SLExpandableTableView.h b/SLExpandableTableView/SLExpandableTableView.h index ff27fd7..cdbd77e 100644 --- a/SLExpandableTableView/SLExpandableTableView.h +++ b/SLExpandableTableView/SLExpandableTableView.h @@ -17,9 +17,13 @@ typedef enum { @protocol UIExpandingTableViewCell +@optional @property (nonatomic, assign, getter = isLoading) BOOL loading; +@optional @property (nonatomic, readonly) UIExpansionStyle expansionStyle; + +@optional - (void)setExpansionStyle:(UIExpansionStyle)style animated:(BOOL)animated; @end diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index ab1ecf9..40f2140 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -226,8 +226,12 @@ - (void)expandSection:(NSInteger)section animated:(BOOL)animated { [self beginUpdates]; UITableViewCell *cell = (UITableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]; - cell.loading = NO; - [cell setExpansionStyle:UIExpansionStyleExpanded animated:YES]; + if ([cell respondsToSelector:@selector(setLoading:)]) { + cell.loading = NO; + } + if ([cell respondsToSelector:@selector(setExpansionStyle:animated:)]) { + [cell setExpansionStyle:UIExpansionStyleExpanded animated:YES]; + } NSMutableArray *insertArray = [NSMutableArray array]; for (int i = 1; i < newRowCount; i++) { @@ -284,9 +288,12 @@ - (void)collapseSection:(NSInteger)section animated:(BOOL)animated { [self beginUpdates]; UITableViewCell *cell = (UITableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]; - cell.loading = NO; - [cell setExpansionStyle:UIExpansionStyleCollapsed animated:YES]; - + if ([cell respondsToSelector:@selector(setLoading:)]) { + cell.loading = NO; + } + if ([cell respondsToSelector:@selector(setExpansionStyle:animated:)]) { + [cell setExpansionStyle:UIExpansionStyleCollapsed animated:YES]; + } NSMutableArray *deleteArray = [NSMutableArray array]; for (int i = 1; i < newRowCount; i++) { [deleteArray addObject:[NSIndexPath indexPathForRow:i inSection:section] ]; @@ -301,9 +308,12 @@ - (void)collapseSection:(NSInteger)section animated:(BOOL)animated { [self.animatingSectionsDictionary removeObjectForKey:@(section)]; - [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] - atScrollPosition:UITableViewScrollPositionTop - animated:animated]; + // By default the header cell is already on screen when it's collapsed. + // If a user wants to programatically collapse a section, they should handle scrolling themselves + // If you disagree feel free to uncomment this :) +// [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] +// atScrollPosition:UITableViewScrollPositionTop +// animated:animated]; void(^completionBlock)(void) = ^{ if ([self respondsToSelector:@selector(scrollViewDidScroll:)]) { @@ -437,13 +447,19 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N if (indexPath.row == 0) { UITableViewCell *cell = [self.myDataSource tableView:self expandingCellForSection:indexPath.section]; if ([self.downloadingSectionsDictionary[key] boolValue]) { - [cell setLoading:YES]; + if ([cell respondsToSelector:@selector(setLoading:)]) { + [cell setLoading:YES]; + } } else { - [cell setLoading:NO]; - if ([self.showingSectionsDictionary[key] boolValue]) { - [cell setExpansionStyle:UIExpansionStyleExpanded animated:NO]; - } else { - [cell setExpansionStyle:UIExpansionStyleCollapsed animated:NO]; + if ([cell respondsToSelector:@selector(setLoading:)]) { + [cell setLoading:NO]; + } + if ([cell respondsToSelector:@selector(setExpansionStyle:animated:)]) { + if ([self.showingSectionsDictionary[key] boolValue]) { + [cell setExpansionStyle:UIExpansionStyleExpanded animated:NO]; + } else { + [cell setExpansionStyle:UIExpansionStyleCollapsed animated:NO]; + } } } return cell;