Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import "STCollapseTableView.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, STCollapseTableViewDelegate>

@property (weak, nonatomic) IBOutlet STCollapseTableView *tableView;

Expand Down Expand Up @@ -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++)
{
Expand All @@ -60,7 +60,7 @@ - (void)setupViewController
}
[self.data addObject:section];
}

self.headers = [[NSMutableArray alloc] init];
for (int i = 0 ; i < [colors count] ; i++)
{
Expand All @@ -73,53 +73,64 @@ - (void)setupViewController
- (void)viewDidLoad
{
[super viewDidLoad];


self.tableView.headerViewTapDelegate = self;
[self.tableView reloadData];
[self.tableView openSection:0 animated:NO];
}

- (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
{
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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<STCollapseTableViewDelegate> 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):
Expand Down
19 changes: 19 additions & 0 deletions STCollapseTableView.podspec
Original file line number Diff line number Diff line change
@@ -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.2" }
s.platform = :ios, '5.0'
s.source_files = 'STCollapseTableView/*.{h,m}'
s.ios.frameworks = ['Foundation', 'UIKit']
s.requires_arc = true

end
14 changes: 13 additions & 1 deletion STCollapseTableView/STCollapseTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
***********************************************************************************/

#import <UIKit/UIKit.h>
@class STCollapseTableView;

@protocol STCollapseTableViewDelegate <NSObject>

@optional

- (void)collapseTableView:(STCollapseTableView *)collapseTableView didOpenSection:(NSInteger)section;
- (void)collapseTableView:(STCollapseTableView *)collapseTableView didCloseSection:(NSInteger)section;

@end

/**
* STCollapseTableView is a UITableView subclass that automatically collapse and/or expand your sections.
Expand Down Expand Up @@ -86,4 +96,6 @@
*/
- (BOOL)isOpenSection:(NSUInteger)sectionIndex;

@end
@property (nonatomic, weak) id<STCollapseTableViewDelegate> headerViewTapDelegate;

@end
Loading