From d2d134cbd2a90f5d8356199da9856ed1b18b83bc Mon Sep 17 00:00:00 2001 From: Pau Ballart Date: Thu, 27 Aug 2015 13:33:18 +0200 Subject: [PATCH] ADD optional methods to the protocol to avoid expanding or collapsing cells --- SLExpandableTableView/SLExpandableTableView.h | 2 ++ SLExpandableTableView/SLExpandableTableView.m | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/SLExpandableTableView/SLExpandableTableView.h b/SLExpandableTableView/SLExpandableTableView.h index ff27fd7..1a1b295 100644 --- a/SLExpandableTableView/SLExpandableTableView.h +++ b/SLExpandableTableView/SLExpandableTableView.h @@ -45,9 +45,11 @@ typedef enum { @optional - (void)tableView:(SLExpandableTableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPathWhileAnimatingSection:(NSIndexPath *)indexPath; +- (BOOL)tableView:(SLExpandableTableView *)tableView shouldExpandSection:(NSUInteger)section animated:(BOOL)animated; - (void)tableView:(SLExpandableTableView *)tableView willExpandSection:(NSUInteger)section animated:(BOOL)animated; - (void)tableView:(SLExpandableTableView *)tableView didExpandSection:(NSUInteger)section animated:(BOOL)animated; +- (BOOL)tableView:(SLExpandableTableView *)tableView shouldCollapseSection:(NSUInteger)section animated:(BOOL)animated; - (void)tableView:(SLExpandableTableView *)tableView willCollapseSection:(NSUInteger)section animated:(BOOL)animated; - (void)tableView:(SLExpandableTableView *)tableView didCollapseSection:(NSUInteger)section animated:(BOOL)animated; diff --git a/SLExpandableTableView/SLExpandableTableView.m b/SLExpandableTableView/SLExpandableTableView.m index ab1ecf9..f4f2eae 100644 --- a/SLExpandableTableView/SLExpandableTableView.m +++ b/SLExpandableTableView/SLExpandableTableView.m @@ -385,12 +385,39 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath // expand cell got clicked if ([self.myDataSource tableView:self needsToDownloadDataForExpandableSection:indexPath.section]) { // we need to download some data first - [self downloadDataInSection:indexPath.section]; + if ([self.myDelegate respondsToSelector:@selector(tableView:shouldExpandSection:animated:)]) { + BOOL shouldExpand = [self.myDelegate tableView:self shouldExpandSection:indexPath.section animated:YES]; + if (shouldExpand) { + [self downloadDataInSection:indexPath.section]; + } else { + [tableView deselectRowAtIndexPath:indexPath animated:NO]; + } + } else { + [self downloadDataInSection:indexPath.section]; + } } else { if ([self.showingSectionsDictionary[key] boolValue]) { - [self collapseSection:indexPath.section animated:YES]; + if ([self.myDelegate respondsToSelector:@selector(tableView:shouldCollapseSection:animated:)]) { + BOOL shouldCollapse = [self.myDelegate tableView:self shouldCollapseSection:indexPath.section animated:YES]; + if (shouldCollapse) { + [self collapseSection:indexPath.section animated:YES]; + } else { + [tableView deselectRowAtIndexPath:indexPath animated:NO]; + } + } else { + [self collapseSection:indexPath.section animated:YES]; + } } else { - [self expandSection:indexPath.section animated:YES]; + if ([self.myDelegate respondsToSelector:@selector(tableView:shouldExpandSection:animated:)]) { + BOOL shouldExpand = [self.myDelegate tableView:self shouldExpandSection:indexPath.section animated:YES]; + if (shouldExpand) { + [self expandSection:indexPath.section animated:YES]; + } else{ + [tableView deselectRowAtIndexPath:indexPath animated:NO]; + } + } else { + [self expandSection:indexPath.section animated:YES]; + } } } } else {