From 389db23aa73cd5f25d0d12434f0bc62fab6f2db3 Mon Sep 17 00:00:00 2001 From: mayqiyue Date: Fri, 1 May 2015 17:43:53 +0800 Subject: [PATCH] Add the delegate:STCollapseTableViewDelegate to handle header tapping events --- .../STCollapseTableViewDemo/ViewController.m | 13 +- README.md | 11 +- STCollapseTableView/STCollapseTableView.h | 11 ++ STCollapseTableView/STCollapseTableView.m | 153 +++++++++--------- 4 files changed, 112 insertions(+), 76 deletions(-) diff --git a/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m b/Demo/STCollapseTableViewDemo/STCollapseTableViewDemo/ViewController.m index 6a7d73c..4d7f728 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; @@ -74,6 +74,7 @@ - (void)viewDidLoad { [super viewDidLoad]; + self.tableView.headerViewTapDelegate = self; [self.tableView reloadData]; [self.tableView openSection:0 animated:NO]; } @@ -122,4 +123,14 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger return [self.headers objectAtIndex:section]; } +- (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 diff --git a/README.md b/README.md index 12809a7..0157872 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,17 @@ As you might have seen, your headers automatically toggle theirs section on a ta This is automatically done for you in three conditions: * Your datasource implements the `tableView:heightForHeaderInSection:` method * The returned views haven't any UITapGestureRecognizer. -* the STCollapseTableView property `shouldHandleHeadersTap` is YES (which is the default value). +* 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..fe91f33 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; +@property (nonatomic, weak) id headerViewTapDelegate; + @end diff --git a/STCollapseTableView/STCollapseTableView.m b/STCollapseTableView/STCollapseTableView.m index 9a3b893..7d5ff0e 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 @@ -118,7 +118,7 @@ - (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 @@ -132,16 +132,16 @@ - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated { 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]; @@ -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,28 +204,28 @@ - (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 @@ -262,41 +262,41 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N - (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]; + int nbSection = (int)[self.collapseDataSource numberOfSectionsInTableView:tableView]; - while (nbSection < [self.sectionsStates count]) + while (nbSection < [self.sectionsStates count]) { - [self.sectionsStates removeLastObject]; - } + [self.sectionsStates removeLastObject]; + } - while (nbSection > [self.sectionsStates count]) + while (nbSection > [self.sectionsStates count]) { - [self.sectionsStates addObject:@NO]; - } + [self.sectionsStates addObject:@NO]; + } - return nbSection; + 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]]) { @@ -307,9 +307,9 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger if (!tapGestureFound) { - [view setTag:section]; [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]]; } + [view setTag:section]; } return view; @@ -322,6 +322,11 @@ - (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]; } }