Skip to content

Commit

Permalink
Initial implementation with sample code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdstroebel committed Oct 13, 2013
1 parent 1e564b7 commit 17c5981
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 3 deletions.
16 changes: 16 additions & 0 deletions MSCollapsibleTableViewController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
CC0BA9B31809A399006F7A78 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = CC0BA9B21809A399006F7A78 /* [email protected] */; };
CC0BA9B51809A399006F7A78 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = CC0BA9B41809A399006F7A78 /* [email protected] */; };
CC0BA9B81809A399006F7A78 /* MSCollapsibleTableViewController.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = CC0BA9B61809A399006F7A78 /* MSCollapsibleTableViewController.xcdatamodeld */; };
CC0BA9BF1809A3BE006F7A78 /* TestStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CC0BA9BE1809A3BE006F7A78 /* TestStoryboard.storyboard */; };
CC0BA9C21809A3E0006F7A78 /* MSCollapsibleTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CC0BA9C11809A3E0006F7A78 /* MSCollapsibleTableViewController.m */; };
CC0BA9C5180AB843006F7A78 /* MSTestTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CC0BA9C4180AB843006F7A78 /* MSTestTableViewController.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -36,6 +39,11 @@
CC0BA9B21809A399006F7A78 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
CC0BA9B41809A399006F7A78 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
CC0BA9B71809A399006F7A78 /* MSCollapsibleTableViewController.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MSCollapsibleTableViewController.xcdatamodel; sourceTree = "<group>"; };
CC0BA9BE1809A3BE006F7A78 /* TestStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = TestStoryboard.storyboard; sourceTree = "<group>"; };
CC0BA9C01809A3E0006F7A78 /* MSCollapsibleTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSCollapsibleTableViewController.h; sourceTree = "<group>"; };
CC0BA9C11809A3E0006F7A78 /* MSCollapsibleTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSCollapsibleTableViewController.m; sourceTree = "<group>"; };
CC0BA9C3180AB843006F7A78 /* MSTestTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSTestTableViewController.h; sourceTree = "<group>"; };
CC0BA9C4180AB843006F7A78 /* MSTestTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSTestTableViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -88,6 +96,11 @@
CC0BA9AE1809A399006F7A78 /* MSAppDelegate.m */,
CC0BA9B61809A399006F7A78 /* MSCollapsibleTableViewController.xcdatamodeld */,
CC0BA9A51809A399006F7A78 /* Supporting Files */,
CC0BA9BE1809A3BE006F7A78 /* TestStoryboard.storyboard */,
CC0BA9C01809A3E0006F7A78 /* MSCollapsibleTableViewController.h */,
CC0BA9C11809A3E0006F7A78 /* MSCollapsibleTableViewController.m */,
CC0BA9C3180AB843006F7A78 /* MSTestTableViewController.h */,
CC0BA9C4180AB843006F7A78 /* MSTestTableViewController.m */,
);
path = MSCollapsibleTableViewController;
sourceTree = "<group>";
Expand Down Expand Up @@ -162,6 +175,7 @@
CC0BA9B11809A399006F7A78 /* Default.png in Resources */,
CC0BA9B31809A399006F7A78 /* [email protected] in Resources */,
CC0BA9B51809A399006F7A78 /* [email protected] in Resources */,
CC0BA9BF1809A3BE006F7A78 /* TestStoryboard.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -175,6 +189,8 @@
CC0BA9AB1809A399006F7A78 /* main.m in Sources */,
CC0BA9AF1809A399006F7A78 /* MSAppDelegate.m in Sources */,
CC0BA9B81809A399006F7A78 /* MSCollapsibleTableViewController.xcdatamodeld in Sources */,
CC0BA9C21809A3E0006F7A78 /* MSCollapsibleTableViewController.m in Sources */,
CC0BA9C5180AB843006F7A78 /* MSTestTableViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 6 additions & 3 deletions MSCollapsibleTableViewController/MSAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ @implementation MSAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TestStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController];

// Set root view controller and make windows visible
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIMainStoryboardFile</key>
<string>TestStoryboard</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MSCollapsibleTableViewController.h
// MSCollapsibleTableViewController
//
// Created by Marc Stroebel on 2013/10/12.
// Copyright (c) 2013 StroebelSoftware. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef NSInteger(^numberOfRowsInSectionBlock)(NSInteger);

@interface MSCollapsibleTableViewController : UITableViewController

@property (nonatomic, readonly) NSArray* collapsedSections;
@property (nonatomic, strong) numberOfRowsInSectionBlock numberOfRowsInSectionBlock;

-(void)toggleCollapseSection:(NSInteger)section;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// MSCollapsibleTableViewController.m
// MSCollapsibleTableViewController
//
// Created by Marc Stroebel on 2013/10/12.
// Copyright (c) 2013 StroebelSoftware. All rights reserved.
//

#import "MSCollapsibleTableViewController.h"

@interface MSCollapsibleTableViewController ()

@end

@implementation MSCollapsibleTableViewController

@synthesize collapsedSections = _collapsedSections;

- (void)viewDidLoad
{
[super viewDidLoad];
_collapsedSections = [NSArray array];
}

#pragma mark - Table View delegate methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSNumber* sectionNumber = [NSNumber numberWithInteger:section];
if ([_collapsedSections containsObject:sectionNumber])
{
return 0;
}

return self.numberOfRowsInSectionBlock(section);
}

#pragma mark - Collapsible Section Methods

-(void)toggleCollapseSection:(NSInteger)section
{
NSNumber* sectionNumber = [NSNumber numberWithInteger:section];
NSMutableArray* mutableCopy = [_collapsedSections mutableCopy];

if ([_collapsedSections containsObject:sectionNumber])
{
[mutableCopy removeObject:sectionNumber];
}
else
{
[mutableCopy addObject:sectionNumber];
}

_collapsedSections = [NSArray arrayWithArray:mutableCopy];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sectionNumber integerValue]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end
13 changes: 13 additions & 0 deletions MSCollapsibleTableViewController/MSTestTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// MSTestTableViewController.h
// MSCollapsibleTableViewController
//
// Created by Marc Stroebel on 2013/10/13.
// Copyright (c) 2013 StroebelSoftware. All rights reserved.
//

#import "MSCollapsibleTableViewController.h"

@interface MSTestTableViewController : MSCollapsibleTableViewController

@end
134 changes: 134 additions & 0 deletions MSCollapsibleTableViewController/MSTestTableViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// MSTestTableViewController.m
// MSCollapsibleTableViewController
//
// Created by Marc Stroebel on 2013/10/13.
// Copyright (c) 2013 StroebelSoftware. All rights reserved.
//

#import "MSTestTableViewController.h"

@interface MSTestTableViewController ()

@property (strong, nonatomic) NSMutableArray* tableDatasource;

@end

@implementation MSTestTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSMutableArray* testDatasource = [NSMutableArray array];
[testDatasource addObject:@"Row 1"];
[testDatasource addObject:@"Row 2"];
[testDatasource addObject:@"Row 3"];
[testDatasource addObject:@"Row 4"];
self.tableDatasource = testDatasource;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Section %d", section + 1];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/*************************************************************************
IMPORTANT: You must create a block for your normal "numberOfRowsInSection"
implementation. This block will be called if the section must
not be collapsed
*************************************************************************/

self.numberOfRowsInSectionBlock = ^(NSInteger section) {
if (section == 0 || section == 1)
{
return 1;
}

return 2;
};

/*************************************************************************
IMPORTANT: You need to make sure you call the super classes implementation of
"numberOfRowsInSection"
*************************************************************************/

return [super tableView:tableView numberOfRowsInSection:section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
headerView.backgroundColor = [UIColor redColor];

UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
headerLabel.backgroundColor = [UIColor redColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];

/**************************************************************************
Start of sample toggling code:
This sets up the tap gesture recognizer for the header view to organise the collapsing
**************************************************************************/

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedHeaderView:)];

// TODO: Find a better way to do this... we need to send information to the action "tappedHeaderView"
tap.accessibilityValue = [NSString stringWithFormat:@"%d", section];

[headerView addGestureRecognizer:tap];

/**************************************************************************
End of sample toggling code
**************************************************************************/

[headerView addSubview:headerLabel];

return headerView;
}

- (void)tappedHeaderView:(UITapGestureRecognizer*)tap
{
[self toggleCollapseSection:[tap.accessibilityValue integerValue]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"testCell"];
NSString* cellData = [self.tableDatasource objectAtIndex:indexPath.row];
cell.textLabel.text = cellData;
cell.detailTextLabel.text = @"";

return cell;
}

@end
54 changes: 54 additions & 0 deletions MSCollapsibleTableViewController/TestStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="11G63" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES" initialViewController="YXl-0B-8Wq">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
</dependencies>
<scenes>
<!--Test Table View Controller-->
<scene sceneID="HaW-xr-93p">
<objects>
<tableViewController id="YXl-0B-8Wq" customClass="MSTestTableViewController" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="NxK-Uc-YdR">
<rect key="frame" x="0.0" y="20" width="768" height="1004"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="testCell" textLabel="QtJ-KI-g4m" detailTextLabel="uPG-48-yrk" style="IBUITableViewCellStyleValue2" id="TfV-tx-wbf">
<rect key="frame" x="0.0" y="22" width="768" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="768" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QtJ-KI-g4m">
<fontDescription key="fontDescription" type="boldSystem" pointSize="12"/>
<color key="textColor" red="0.32156862745098042" green="0.40000000000000002" blue="0.56862745098039214" alpha="1" colorSpace="calibratedRGB"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Detail" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uPG-48-yrk">
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="YXl-0B-8Wq" id="IfJ-tj-RUD"/>
<outlet property="delegate" destination="YXl-0B-8Wq" id="vhz-fv-9d9"/>
</connections>
</tableView>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="J2O-2U-0Wf" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="12" y="-270"/>
</scene>
</scenes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar" statusBarStyle="blackTranslucent"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination"/>
</simulatedMetricsContainer>
</document>

0 comments on commit 17c5981

Please sign in to comment.