From 58d0ab3baac7611a6d3d8ff3d13d9c887da9c583 Mon Sep 17 00:00:00 2001 From: Oleg Date: Fri, 27 Jun 2014 16:52:57 +0200 Subject: [PATCH 1/8] deleteSections: and insertSections: methods overloaded, some glitches fixed --- STCollapseTableView/STCollapseTableView.m | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 9a3b893..28eab16 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -34,8 +34,8 @@ @interface STCollapseTableView () -@property (nonatomic, assign) id collapseDataSource; -@property (nonatomic, assign) id collapseDelegate; +@property (nonatomic, weak) id collapseDataSource; +@property (nonatomic, weak) id collapseDelegate; @property (nonatomic, strong) NSMutableArray* sectionsStates; @end @@ -253,6 +253,23 @@ - (void)setExclusiveSections:(BOOL)exclusiveSections } } +#pragma mark managing section updates (overload table view a bit...) + +- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation +{ + [self.sectionsStates removeObjectsAtIndexes: sections]; + [super deleteSections:sections withRowAnimation:animation]; +} + +- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation +{ + [sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { + [self.sectionsStates insertObject:@NO atIndex:idx]; + }]; + + [super insertSections:sections withRowAnimation:animation]; +} + #pragma mark - DataSource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @@ -271,7 +288,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - int nbSection = [self.collapseDataSource numberOfSectionsInTableView:tableView]; + NSInteger nbSection = [self.collapseDataSource numberOfSectionsInTableView:tableView]; while (nbSection < [self.sectionsStates count]) { From 15e537c0f07277e08c3eb4a5a5440d23610664ed Mon Sep 17 00:00:00 2001 From: Joseph Donegan Date: Wed, 7 Oct 2015 11:39:04 +0100 Subject: [PATCH 2/8] Fix for https://github.com/iSofTom/STCollapseTableView/issues/9 --- STCollapseTableView/STCollapseTableView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 9a3b893..30d9b6a 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -91,7 +91,7 @@ - (void)setDataSource:(id )newDataSource - (void)setDelegate:(id)newDelegate { - if (newDelegate != self.collapseDelegate) + if (newDelegate && newDelegate != self.collapseDelegate) { self.collapseDelegate = newDelegate; [super setDelegate:self.collapseDelegate?self:nil]; From acfb60ac0f44c2ea084108adb949c02bd6d3ba1a Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 14:12:44 +0100 Subject: [PATCH 3/8] Fixes #6: now it works ok with reusable headers --- STCollapseTableView/STCollapseTableView.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 9a3b893..b2c1705 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -307,9 +307,10 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger if (!tapGestureFound) { - [view setTag:section]; [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]]; } + // Updating tag for reusable headers: https://github.com/iSofTom/STCollapseTableView/issues/6 + [view setTag:section]; } return view; From df78e650183a6f7992ec767e2361ef9300306a60 Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 14:33:38 +0100 Subject: [PATCH 4/8] Adds podspec --- STCollapseTableView.podspec | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 STCollapseTableView.podspec diff --git a/STCollapseTableView.podspec b/STCollapseTableView.podspec new file mode 100644 index 0000000..ae9acdb --- /dev/null +++ b/STCollapseTableView.podspec @@ -0,0 +1,19 @@ +Pod::Spec.new do |s| + s.name = "STCollapseTableView" + s.version = "0.1.0" + s.summary = "A UITableView subclass that automatically collapse and/or expand your sections." + s.description = <<-DESC + A UITableView subclass that automatically collapse and/or expand your sections. + You just have to fill your datasource like for a classic UITableView and the magic will happen., + DESC + + s.homepage = "https://github.com/iSofTom/STCollapseTableView" + s.license = "MIT" + s.authors = { "iSofTom" => "thomas@isoftom.com" } + s.source = { :git => "https://github.com/iSofTom/STCollapseTableView.git", :tag => "0.1.0" } + s.platform = :ios, '5.0' + s.source_files = 'STCollapseTableView/*.{h,m}' + s.ios.frameworks = ['Foundation', 'UIKit'] + s.requires_arc = true + +end From e752cf4a67c331b623dafb6e249dd1db32548465 Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 14:44:55 +0100 Subject: [PATCH 5/8] Updates podspec for v0.1.2 --- STCollapseTableView.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STCollapseTableView.podspec b/STCollapseTableView.podspec index ae9acdb..71d4c96 100644 --- a/STCollapseTableView.podspec +++ b/STCollapseTableView.podspec @@ -10,7 +10,7 @@ Pod::Spec.new do |s| s.homepage = "https://github.com/iSofTom/STCollapseTableView" s.license = "MIT" s.authors = { "iSofTom" => "thomas@isoftom.com" } - s.source = { :git => "https://github.com/iSofTom/STCollapseTableView.git", :tag => "0.1.0" } + s.source = { :git => "https://github.com/iSofTom/STCollapseTableView.git", :tag => "0.1.2" } s.platform = :ios, '5.0' s.source_files = 'STCollapseTableView/*.{h,m}' s.ios.frameworks = ['Foundation', 'UIKit'] From 4dec8216ffd768f0539100720a6f72425cc77927 Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 15:00:39 +0100 Subject: [PATCH 6/8] Copies changes from pull request 12: https://github.com/iSofTom/STCollapseTableView/pull/12 --- .../STCollapseTableViewDemo/ViewController.m | 53 +++-- README.md | 11 +- STCollapseTableView/STCollapseTableView.h | 13 +- STCollapseTableView/STCollapseTableView.m | 222 +++++++++--------- 4 files changed, 167 insertions(+), 132 deletions(-) diff --git a/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m b/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m index 6a7d73c..8fe5827 100644 --- a/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m +++ b/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m @@ -10,7 +10,7 @@ #import "STCollapseTableView.h" -@interface ViewController () +@interface ViewController () @property (weak, nonatomic) IBOutlet STCollapseTableView *tableView; @@ -49,7 +49,7 @@ - (void)setupViewController [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor]]; - + self.data = [[NSMutableArray alloc] init]; for (int i = 0 ; i < [colors count] ; i++) { @@ -60,7 +60,7 @@ - (void)setupViewController } [self.data addObject:section]; } - + self.headers = [[NSMutableArray alloc] init]; for (int i = 0 ; i < [colors count] ; i++) { @@ -73,7 +73,8 @@ - (void)setupViewController - (void)viewDidLoad { [super viewDidLoad]; - + + self.tableView.headerViewTapDelegate = self; [self.tableView reloadData]; [self.tableView openSection:0 animated:NO]; } @@ -81,40 +82,40 @@ - (void)viewDidLoad - (IBAction)handleExclusiveButtonTap:(UIButton*)button { [self.tableView setExclusiveSections:!self.tableView.exclusiveSections]; - + [button setTitle:self.tableView.exclusiveSections?@"exclusive":@"!exclusive" forState:UIControlStateNormal]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return [self.data count]; + return [self.data count]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - static NSString* cellIdentifier = @"cell"; - - UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - - if (!cell) + static NSString* cellIdentifier = @"cell"; + + UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; + + if (!cell) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; - } - - NSString* text = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; - cell.textLabel.text = text; - - return cell; + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; + } + + NSString* text = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; + cell.textLabel.text = text; + + return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return [[self.data objectAtIndex:section] count]; + return [[self.data objectAtIndex:section] count]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { - return 40; + return 40; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section @@ -122,4 +123,14 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger return [self.headers objectAtIndex:section]; } -@end +- (void)STCollapseTableView:(STCollapseTableView *)STCollapseTableView didSelectHeaderViewAtSection:(NSInteger)section +{ + [[[UIAlertView alloc] initWithTitle:@"" + message:[NSString stringWithFormat:@"headerView %ld tapped",section] + delegate:nil + cancelButtonTitle:@"OK" + otherButtonTitles:nil, nil] + show]; +} + +@end \ No newline at end of file diff --git a/README.md b/README.md index 12809a7..5a48520 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ so if you want the first section to be open after your view is loaded, you could - (void)viewDidLoad { [super viewDidLoad]; - + [self.tableView reloadData]; [self.tableView openSection:0 animated:NO]; } @@ -43,6 +43,15 @@ This is automatically done for you in three conditions: * The returned views haven't any UITapGestureRecognizer. * the STCollapseTableView property `shouldHandleHeadersTap` is YES (which is the default value). +---- +``` +@property (nonatomic, weak) id headerViewTapDelegate; +``` +Just as the name, the herderViewTapDelegate is designed to handle hederViews tapping events, you can do things in the delegate method when a specified headerView tapped: + +``` +- (void)STCollapseTableView:(STCollapseTableView *)STCollapseTableView didSelectHeaderViewAtSection:(NSInteger)section; +``` ## Installation To include this component in your project, I recommend you to use [Cocoapods](http://cocoapods.org): diff --git a/STCollapseTableView/STCollapseTableView.h b/STCollapseTableView/STCollapseTableView.h index 6b7e99e..54e6e7a 100644 --- a/STCollapseTableView/STCollapseTableView.h +++ b/STCollapseTableView/STCollapseTableView.h @@ -29,6 +29,15 @@ ***********************************************************************************/ #import +@class STCollapseTableView; + +@protocol STCollapseTableViewDelegate + +@optional + +- (void)STCollapseTableView:(STCollapseTableView *)STCollapseTableView didSelectHeaderViewAtSection:(NSInteger)section; + +@end /** * STCollapseTableView is a UITableView subclass that automatically collapse and/or expand your sections. @@ -86,4 +95,6 @@ */ - (BOOL)isOpenSection:(NSUInteger)sectionIndex; -@end +@property (nonatomic, weak) id headerViewTapDelegate; + +@end \ No newline at end of file diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index b2c1705..f993ec5 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -34,9 +34,9 @@ @interface STCollapseTableView () -@property (nonatomic, assign) id collapseDataSource; -@property (nonatomic, assign) id collapseDelegate; -@property (nonatomic, strong) NSMutableArray* sectionsStates; +@property (nonatomic, weak) id collapseDataSource; +@property (nonatomic, weak) id collapseDelegate; +@property (nonatomic, strong) NSMutableArray *sectionsStates; @end @@ -44,54 +44,54 @@ @implementation STCollapseTableView - (id)initWithCoder:(NSCoder *)aDecoder { - self = [super initWithCoder:aDecoder]; - if (self) + self = [super initWithCoder:aDecoder]; + if (self) { - [self setupCollapseTableView]; - } - return self; + [self setupCollapseTableView]; + } + return self; } - (id)initWithFrame:(CGRect)frame { - self = [super initWithFrame:frame]; - if (self) + self = [super initWithFrame:frame]; + if (self) { - [self setupCollapseTableView]; - } - return self; + [self setupCollapseTableView]; + } + return self; } - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style { - self = [super initWithFrame:frame style:style]; - if (self) + self = [super initWithFrame:frame style:style]; + if (self) { - [self setupCollapseTableView]; - } - return self; + [self setupCollapseTableView]; + } + return self; } - (void)setupCollapseTableView { - self.exclusiveSections = YES; + self.exclusiveSections = YES; self.shouldHandleHeadersTap = YES; - self.sectionsStates = [[NSMutableArray alloc] init]; + self.sectionsStates = [[NSMutableArray alloc] init]; } - (void)setDataSource:(id )newDataSource { - if (newDataSource != self.collapseDataSource) + if (newDataSource && newDataSource != self.collapseDataSource) { - self.collapseDataSource = newDataSource; - [self.sectionsStates removeAllObjects]; - [super setDataSource:self.collapseDataSource?self:nil]; - } + self.collapseDataSource = newDataSource; + [self.sectionsStates removeAllObjects]; + [super setDataSource:self.collapseDataSource?self:nil]; + } } - (void)setDelegate:(id)newDelegate { - if (newDelegate != self.collapseDelegate) + if (newDelegate && newDelegate != self.collapseDelegate) { self.collapseDelegate = newDelegate; [super setDelegate:self.collapseDelegate?self:nil]; @@ -100,15 +100,15 @@ - (void)setDelegate:(id)newDelegate - (id)forwardingTargetForSelector:(SEL)aSelector { - if ([self.collapseDataSource respondsToSelector:aSelector]) + if ([self.collapseDataSource respondsToSelector:aSelector]) { - return self.collapseDataSource; - } + return self.collapseDataSource; + } if ([self.collapseDelegate respondsToSelector:aSelector]) { return self.collapseDelegate; } - return nil; + return nil; } - (BOOL)respondsToSelector:(SEL)aSelector @@ -117,8 +117,8 @@ - (BOOL)respondsToSelector:(SEL)aSelector { return [self.collapseDelegate respondsToSelector:aSelector]; } - - return [super respondsToSelector:aSelector] || [self.collapseDataSource respondsToSelector:aSelector] || [self.collapseDelegate respondsToSelector:aSelector]; + + return [super respondsToSelector:aSelector] || [self.collapseDataSource respondsToSelector:aSelector] || [self.collapseDelegate respondsToSelector:aSelector]; } - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated @@ -127,27 +127,27 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated { return; } - + if ([[self.sectionsStates objectAtIndex:sectionIndex] boolValue]) { return; } - - if (self.exclusiveSections) + + if (self.exclusiveSections) { NSUInteger openedSection = [self openedSection]; - - [self setSectionAtIndex:sectionIndex open:YES]; - [self setSectionAtIndex:openedSection open:NO]; - + + [self setSectionAtIndex:sectionIndex open:YES]; + [self setSectionAtIndex:openedSection open:NO]; + if(animated) - { + { NSArray* indexPathsToInsert = [self indexPathsForRowsInSectionAtIndex:sectionIndex]; NSArray* indexPathsToDelete = [self indexPathsForRowsInSectionAtIndex:openedSection]; - + UITableViewRowAnimation insertAnimation; UITableViewRowAnimation deleteAnimation; - + if (openedSection == NSNotFound || sectionIndex < openedSection) { insertAnimation = UITableViewRowAnimationTop; @@ -158,7 +158,7 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated insertAnimation = UITableViewRowAnimationBottom; deleteAnimation = UITableViewRowAnimationTop; } - + [self beginUpdates]; [self insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation]; [self deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation]; @@ -168,31 +168,31 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated { [self reloadData]; } - } + } else { - [self setSectionAtIndex:sectionIndex open:YES]; - - if (animated) + [self setSectionAtIndex:sectionIndex open:YES]; + + if (animated) { NSArray* indexPathsToInsert = [self indexPathsForRowsInSectionAtIndex:sectionIndex]; - [self insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop]; + [self insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade]; } else { [self reloadData]; } - } + } } - (void)closeSection:(NSUInteger)sectionIndex animated:(BOOL)animated { [self setSectionAtIndex:sectionIndex open:NO]; - - if (animated) + + if (animated) { NSArray* indexPathsToDelete = [self indexPathsForRowsInSectionAtIndex:sectionIndex]; - [self deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop]; + [self deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:NO]; } else { @@ -204,38 +204,38 @@ - (void)toggleSection:(NSUInteger)sectionIndex animated:(BOOL)animated { if (sectionIndex >= [self.sectionsStates count]) { - return; - } - - BOOL sectionIsOpen = [[self.sectionsStates objectAtIndex:sectionIndex] boolValue]; - - if (sectionIsOpen) + return; + } + + BOOL sectionIsOpen = [[self.sectionsStates objectAtIndex:sectionIndex] boolValue]; + + if (sectionIsOpen) { - [self closeSection:sectionIndex animated:animated]; - } + [self closeSection:sectionIndex animated:animated]; + } else { - [self openSection:sectionIndex animated:animated]; - } + [self openSection:sectionIndex animated:animated]; + } } - (BOOL)isOpenSection:(NSUInteger)sectionIndex { if (sectionIndex >= [self.sectionsStates count]) { - return NO; - } - return [[self.sectionsStates objectAtIndex:sectionIndex] boolValue]; + return NO; + } + return [[self.sectionsStates objectAtIndex:sectionIndex] boolValue]; } - (void)setExclusiveSections:(BOOL)exclusiveSections { _exclusiveSections = exclusiveSections; - + if (self.exclusiveSections) { NSInteger firstSection = NSNotFound; - + for (NSUInteger index = 0 ; index < [self.sectionsStates count] ; index++) { if ([[self.sectionsStates objectAtIndex:index] boolValue]) @@ -257,46 +257,46 @@ - (void)setExclusiveSections:(BOOL)exclusiveSections - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - return [self.collapseDataSource tableView:tableView cellForRowAtIndexPath:indexPath]; + return [self.collapseDataSource tableView:tableView cellForRowAtIndexPath:indexPath]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - if ([[self.sectionsStates objectAtIndex:section] boolValue]) + if ([[self.sectionsStates objectAtIndex:section] boolValue]) { - return [self.collapseDataSource tableView:tableView numberOfRowsInSection:section]; - } - return 0; + return [self.collapseDataSource tableView:tableView numberOfRowsInSection:section]; + } + return 0; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - int nbSection = [self.collapseDataSource numberOfSectionsInTableView:tableView]; - - while (nbSection < [self.sectionsStates count]) + int nbSection = (int)[self.collapseDataSource numberOfSectionsInTableView:tableView]; + + while (nbSection < [self.sectionsStates count]) { - [self.sectionsStates removeLastObject]; - } - - while (nbSection > [self.sectionsStates count]) + [self.sectionsStates removeLastObject]; + } + + while (nbSection > [self.sectionsStates count]) { - [self.sectionsStates addObject:@NO]; - } - - return nbSection; + [self.sectionsStates addObject:@NO]; + } + + return nbSection; } #pragma mark - Delegate - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { - UIView* view = [self.collapseDelegate tableView:tableView viewForHeaderInSection:section]; - + UIView *view = [self.collapseDelegate tableView:tableView viewForHeaderInSection:section]; + if (self.shouldHandleHeadersTap) { - NSArray* gestures = view.gestureRecognizers; + NSArray *gestures = view.gestureRecognizers; BOOL tapGestureFound = NO; - for (UIGestureRecognizer* gesture in gestures) + for (UIGestureRecognizer *gesture in gestures) { if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { @@ -304,15 +304,14 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger break; } } - + if (!tapGestureFound) { [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]]; } - // Updating tag for reusable headers: https://github.com/iSofTom/STCollapseTableView/issues/6 [view setTag:section]; } - + return view; } @@ -323,37 +322,42 @@ - (void)handleTapGesture:(UITapGestureRecognizer*)tap NSInteger index = tap.view.tag; if (index >= 0) { + if([self.headerViewTapDelegate respondsToSelector:@selector(STCollapseTableView:didSelectHeaderViewAtSection:)]) + { + [self.headerViewTapDelegate STCollapseTableView:self didSelectHeaderViewAtSection:index]; + } + [self toggleSection:(NSUInteger)index animated:YES]; } } - (NSArray*)indexPathsForRowsInSectionAtIndex:(NSUInteger)sectionIndex { - if (sectionIndex >= [self.sectionsStates count]) + if (sectionIndex >= [self.sectionsStates count]) { - return nil; - } - - NSInteger numberOfRows = [self.collapseDataSource tableView:self numberOfRowsInSection:sectionIndex]; - - NSMutableArray* array = [[NSMutableArray alloc] init]; - - for (int i = 0 ; i < numberOfRows ; i++) + return nil; + } + + NSInteger numberOfRows = [self.collapseDataSource tableView:self numberOfRowsInSection:sectionIndex]; + + NSMutableArray* array = [[NSMutableArray alloc] init]; + + for (int i = 0 ; i < numberOfRows ; i++) { - [array addObject:[NSIndexPath indexPathForRow:i inSection:sectionIndex]]; - } - + [array addObject:[NSIndexPath indexPathForRow:i inSection:sectionIndex]]; + } + return array; } - (void)setSectionAtIndex:(NSUInteger)sectionIndex open:(BOOL)open { - if (sectionIndex >= [self.sectionsStates count]) + if (sectionIndex >= [self.sectionsStates count]) { - return; - } - - [self.sectionsStates replaceObjectAtIndex:sectionIndex withObject:@(open)]; + return; + } + + [self.sectionsStates replaceObjectAtIndex:sectionIndex withObject:@(open)]; } - (NSUInteger)openedSection @@ -362,7 +366,7 @@ - (NSUInteger)openedSection { return NSNotFound; } - + for (NSUInteger index = 0 ; index < [self.sectionsStates count] ; index++) { if ([[self.sectionsStates objectAtIndex:index] boolValue]) @@ -374,4 +378,4 @@ - (NSUInteger)openedSection return NSNotFound; } -@end +@end \ No newline at end of file From 23bda0fa420cdac1fed59c1ffe1c7d15c35ea071 Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 15:17:01 +0100 Subject: [PATCH 7/8] Renames delegate method - makes compile it in Swift --- STCollapseTableView/STCollapseTableView.h | 2 +- STCollapseTableView/STCollapseTableView.m | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/STCollapseTableView/STCollapseTableView.h b/STCollapseTableView/STCollapseTableView.h index 54e6e7a..579ad69 100644 --- a/STCollapseTableView/STCollapseTableView.h +++ b/STCollapseTableView/STCollapseTableView.h @@ -35,7 +35,7 @@ @optional -- (void)STCollapseTableView:(STCollapseTableView *)STCollapseTableView didSelectHeaderViewAtSection:(NSInteger)section; +- (void)collapseTableView:(STCollapseTableView *)collapseTableView didSelectHeaderViewAtSection:(NSInteger)section; @end diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 94bc798..72b760a 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -266,7 +266,7 @@ - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAn [sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { [self.sectionsStates insertObject:@NO atIndex:idx]; }]; - + [super insertSections:sections withRowAnimation:animation]; } @@ -339,9 +339,9 @@ - (void)handleTapGesture:(UITapGestureRecognizer*)tap NSInteger index = tap.view.tag; if (index >= 0) { - if([self.headerViewTapDelegate respondsToSelector:@selector(STCollapseTableView:didSelectHeaderViewAtSection:)]) + if([self.headerViewTapDelegate respondsToSelector:@selector(collapseTableView:didSelectHeaderViewAtSection:)]) { - [self.headerViewTapDelegate STCollapseTableView:self didSelectHeaderViewAtSection:index]; + [self.headerViewTapDelegate collapseTableView:self didSelectHeaderViewAtSection:index]; } [self toggleSection:(NSUInteger)index animated:YES]; From e555570c7863ceb53d07707062cc631965e7f37e Mon Sep 17 00:00:00 2001 From: derpoliuk Date: Wed, 27 Jan 2016 15:55:46 +0100 Subject: [PATCH 8/8] STCollapseTableViewDelegate: adds didClose/didOpen methods and removes didSelect method --- STCollapseTableView/STCollapseTableView.h | 3 ++- STCollapseTableView/STCollapseTableView.m | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/STCollapseTableView/STCollapseTableView.h b/STCollapseTableView/STCollapseTableView.h index 579ad69..1379155 100644 --- a/STCollapseTableView/STCollapseTableView.h +++ b/STCollapseTableView/STCollapseTableView.h @@ -35,7 +35,8 @@ @optional -- (void)collapseTableView:(STCollapseTableView *)collapseTableView didSelectHeaderViewAtSection:(NSInteger)section; +- (void)collapseTableView:(STCollapseTableView *)collapseTableView didOpenSection:(NSInteger)section; +- (void)collapseTableView:(STCollapseTableView *)collapseTableView didCloseSection:(NSInteger)section; @end diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 72b760a..c217e34 100644 --- a/STCollapseTableView/STCollapseTableView.m +++ b/STCollapseTableView/STCollapseTableView.m @@ -133,6 +133,10 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated return; } + if ([self.headerViewTapDelegate respondsToSelector:@selector(collapseTableView:didOpenSection:)]) { + [self.headerViewTapDelegate collapseTableView:self didOpenSection:sectionIndex]; + } + if (self.exclusiveSections) { NSUInteger openedSection = [self openedSection]; @@ -188,6 +192,9 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated - (void)closeSection:(NSUInteger)sectionIndex animated:(BOOL)animated { [self setSectionAtIndex:sectionIndex open:NO]; + if ([self.headerViewTapDelegate respondsToSelector:@selector(collapseTableView:didCloseSection:)]) { + [self.headerViewTapDelegate collapseTableView:self didCloseSection:sectionIndex]; + } if (animated) { @@ -339,11 +346,6 @@ - (void)handleTapGesture:(UITapGestureRecognizer*)tap NSInteger index = tap.view.tag; if (index >= 0) { - if([self.headerViewTapDelegate respondsToSelector:@selector(collapseTableView:didSelectHeaderViewAtSection:)]) - { - [self.headerViewTapDelegate collapseTableView:self didSelectHeaderViewAtSection:index]; - } - [self toggleSection:(NSUInteger)index animated:YES]; } }