Skip to content

Commit 08e58e0

Browse files
author
Darcy Liu
committed
add Accessory
1 parent 2d61302 commit 08e58e0

23 files changed

Lines changed: 1157 additions & 1 deletion

Accessory.zip

37.2 KB
Binary file not shown.

Accessory/Accessory.xcodeproj/project.pbxproj

Lines changed: 315 additions & 0 deletions
Large diffs are not rendered by default.

Accessory/AppDelegate.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
File: AppDelegate.h
3+
Abstract: The application delegate class used for installing our table view controller.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import <UIKit/UIKit.h>
49+
50+
@interface AppDelegate : NSObject <UIApplicationDelegate>
51+
{
52+
UIWindow *window;
53+
UINavigationController *myNavController;
54+
}
55+
56+
@property (nonatomic, retain) IBOutlet UIWindow *window;
57+
@property (nonatomic, retain) IBOutlet UINavigationController *myNavController;
58+
59+
@end

Accessory/AppDelegate.m

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
File: AppDelegate.m
3+
Abstract: The application delegate class used for installing our table view controller.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import "AppDelegate.h"
49+
50+
@implementation AppDelegate;
51+
52+
@synthesize window, myNavController;
53+
54+
55+
- (void)dealloc
56+
{
57+
[myNavController release];
58+
[window release];
59+
60+
[super dealloc];
61+
}
62+
63+
64+
- (void)applicationDidFinishLaunching:(UIApplication *)application
65+
{
66+
// create window and set up table view controller
67+
[window addSubview:myNavController.view];
68+
69+
[window makeKeyAndVisible];
70+
}
71+
72+
@end

Accessory/Info.plist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>${PRODUCT_NAME}</string>
9+
<key>CFBundleExecutable</key>
10+
<string>${EXECUTABLE_NAME}</string>
11+
<key>CFBundleIconFiles</key>
12+
<array>
13+
<string>Icon.png</string>
14+
<string>[email protected]</string>
15+
<string>Icon-72.png</string>
16+
<string>Icon-Small.png</string>
17+
<string>Icon-Small-50.png</string>
18+
<string>[email protected]</string>
19+
</array>
20+
<key>CFBundleIdentifier</key>
21+
<string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
22+
<key>CFBundleInfoDictionaryVersion</key>
23+
<string>6.0</string>
24+
<key>CFBundleName</key>
25+
<string>${PRODUCT_NAME}</string>
26+
<key>CFBundlePackageType</key>
27+
<string>APPL</string>
28+
<key>CFBundleSignature</key>
29+
<string>????</string>
30+
<key>CFBundleVersion</key>
31+
<string>1.2</string>
32+
<key>NSMainNibFile</key>
33+
<string>MainWindow</string>
34+
<key>LSRequiresIPhoneOS</key>
35+
<true/>
36+
</dict>
37+
</plist>

Accessory/MyTableViewController.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
File: MyTableViewController.h
3+
Abstract: The main table view controller of this app.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import <UIKit/UIKit.h>
49+
50+
@interface MyTableViewController : UITableViewController
51+
{
52+
@private
53+
NSMutableArray *dataArray;
54+
}
55+
56+
@end

Accessory/MyTableViewController.m

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
File: MyTableViewController.m
3+
Abstract: The main table view controller of this app.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import "MyTableViewController.h"
49+
50+
@interface MyTableViewController ()
51+
@property (nonatomic, retain) NSMutableArray *dataArray;
52+
@end
53+
54+
@implementation MyTableViewController
55+
56+
@synthesize dataArray;
57+
58+
- (void)viewDidLoad
59+
{
60+
// load our data from a plist file inside our app bundle
61+
NSString *path = [[NSBundle mainBundle] pathForResource:@"tableData" ofType:@"plist"];
62+
self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];
63+
}
64+
65+
66+
// called after the view controller's view is released and set to nil.
67+
// For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
68+
// So release any properties that are loaded in viewDidLoad or can be recreated lazily.
69+
//
70+
- (void)viewDidUnload
71+
{
72+
self.dataArray = nil;
73+
}
74+
75+
76+
- (void)dealloc
77+
{
78+
[dataArray release];
79+
[super dealloc];
80+
}
81+
82+
83+
#pragma mark - UITableView delegate methods
84+
85+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
86+
{
87+
return [dataArray count];
88+
}
89+
90+
91+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
92+
{
93+
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
94+
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
95+
}
96+
97+
98+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
99+
{
100+
static NSString *kCustomCellID = @"MyCellID";
101+
102+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
103+
if (cell == nil)
104+
{
105+
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
106+
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
107+
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
108+
}
109+
110+
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
111+
cell.textLabel.text = [item objectForKey:@"text"];
112+
113+
[item setObject:cell forKey:@"cell"];
114+
115+
BOOL checked = [[item objectForKey:@"checked"] boolValue];
116+
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
117+
118+
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
119+
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
120+
button.frame = frame; // match the button's size with the image size
121+
122+
[button setBackgroundImage:image forState:UIControlStateNormal];
123+
124+
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
125+
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
126+
cell.backgroundColor = [UIColor clearColor];
127+
cell.accessoryView = button;
128+
129+
return cell;
130+
}
131+
132+
133+
- (void)checkButtonTapped:(id)sender event:(id)event
134+
{
135+
NSSet *touches = [event allTouches];
136+
UITouch *touch = [touches anyObject];
137+
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
138+
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
139+
if (indexPath != nil)
140+
{
141+
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
142+
}
143+
}
144+
145+
146+
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
147+
{
148+
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
149+
150+
BOOL checked = [[item objectForKey:@"checked"] boolValue];
151+
152+
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
153+
154+
UITableViewCell *cell = [item objectForKey:@"cell"];
155+
UIButton *button = (UIButton *)cell.accessoryView;
156+
157+
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
158+
[button setBackgroundImage:newImage forState:UIControlStateNormal];
159+
}
160+
161+
@end
162+

Accessory/Prefix.pch

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Prefix header for all source files of the 'Accessory' target in the 'Accessory' project
3+
//
4+
5+
#ifdef __OBJC__
6+
#import <Foundation/Foundation.h>
7+
#import <UIKit/UIKit.h>
8+
#endif

0 commit comments

Comments
 (0)