Skip to content

Commit 49f6e38

Browse files
author
Darcy Liu
committed
add DateSectionTitles
1 parent 0f78331 commit 49f6e38

24 files changed

+1935
-0
lines changed

DateSectionTitles.zip

41.8 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*
3+
File: DateSectionTitlesAppDelegate.h
4+
Abstract: Table view controller to display Events by section.
5+
The application delegate is almost unchanged from that provided by the standard Xcode template, except that application data is created in the persistentStoreCoordinator method.
6+
7+
Version: 1.2
8+
9+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
10+
Inc. ("Apple") in consideration of your agreement to the following
11+
terms, and your use, installation, modification or redistribution of
12+
this Apple software constitutes acceptance of these terms. If you do
13+
not agree with these terms, please do not use, install, modify or
14+
redistribute this Apple software.
15+
16+
In consideration of your agreement to abide by the following terms, and
17+
subject to these terms, Apple grants you a personal, non-exclusive
18+
license, under Apple's copyrights in this original Apple software (the
19+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
20+
Software, with or without modifications, in source and/or binary forms;
21+
provided that if you redistribute the Apple Software in its entirety and
22+
without modifications, you must retain this notice and the following
23+
text and disclaimers in all such redistributions of the Apple Software.
24+
Neither the name, trademarks, service marks or logos of Apple Inc. may
25+
be used to endorse or promote products derived from the Apple Software
26+
without specific prior written permission from Apple. Except as
27+
expressly stated in this notice, no other rights or licenses, express or
28+
implied, are granted by Apple herein, including but not limited to any
29+
patent rights that may be infringed by your derivative works or by other
30+
works in which the Apple Software may be incorporated.
31+
32+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37+
38+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45+
POSSIBILITY OF SUCH DAMAGE.
46+
47+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
48+
49+
*/
50+
51+
@interface DateSectionTitlesAppDelegate : NSObject <UIApplicationDelegate> {
52+
53+
NSManagedObjectModel *managedObjectModel;
54+
NSManagedObjectContext *managedObjectContext;
55+
NSPersistentStoreCoordinator *persistentStoreCoordinator;
56+
57+
UIWindow *window;
58+
UINavigationController *navigationController;
59+
}
60+
61+
@property (nonatomic, retain) IBOutlet UIWindow *window;
62+
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
63+
64+
- (NSString *)applicationDocumentsDirectory;
65+
66+
@end
67+
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
2+
/*
3+
File: DateSectionTitlesAppDelegate.m
4+
Abstract: Table view controller to display Events by section.
5+
The application delegate is almost unchanged from that provided by the standard Xcode template, except that application data is created in the persistentStoreCoordinator method.
6+
7+
Version: 1.2
8+
9+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
10+
Inc. ("Apple") in consideration of your agreement to the following
11+
terms, and your use, installation, modification or redistribution of
12+
this Apple software constitutes acceptance of these terms. If you do
13+
not agree with these terms, please do not use, install, modify or
14+
redistribute this Apple software.
15+
16+
In consideration of your agreement to abide by the following terms, and
17+
subject to these terms, Apple grants you a personal, non-exclusive
18+
license, under Apple's copyrights in this original Apple software (the
19+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
20+
Software, with or without modifications, in source and/or binary forms;
21+
provided that if you redistribute the Apple Software in its entirety and
22+
without modifications, you must retain this notice and the following
23+
text and disclaimers in all such redistributions of the Apple Software.
24+
Neither the name, trademarks, service marks or logos of Apple Inc. may
25+
be used to endorse or promote products derived from the Apple Software
26+
without specific prior written permission from Apple. Except as
27+
expressly stated in this notice, no other rights or licenses, express or
28+
implied, are granted by Apple herein, including but not limited to any
29+
patent rights that may be infringed by your derivative works or by other
30+
works in which the Apple Software may be incorporated.
31+
32+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37+
38+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45+
POSSIBILITY OF SUCH DAMAGE.
46+
47+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
48+
49+
*/
50+
51+
#import "DateSectionTitlesAppDelegate.h"
52+
#import "RootViewController.h"
53+
54+
#import "Event.h"
55+
56+
@interface DateSectionTitlesAppDelegate (CoreDateStack)
57+
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
58+
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
59+
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
60+
@end
61+
62+
63+
@implementation DateSectionTitlesAppDelegate
64+
65+
@synthesize window;
66+
@synthesize navigationController;
67+
68+
69+
#pragma mark -
70+
#pragma mark Application lifecycle
71+
72+
- (void)applicationDidFinishLaunching:(UIApplication *)application {
73+
74+
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
75+
rootViewController.managedObjectContext = self.managedObjectContext;
76+
77+
[window addSubview:[navigationController view]];
78+
[window makeKeyAndVisible];
79+
}
80+
81+
82+
- (void)applicationWillTerminate:(UIApplication *)application {
83+
84+
NSError *error = nil;
85+
if (managedObjectContext != nil) {
86+
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
87+
/*
88+
Replace this implementation with code to handle the error appropriately.
89+
90+
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
91+
*/
92+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
93+
abort();
94+
}
95+
}
96+
}
97+
98+
99+
#pragma mark -
100+
#pragma mark Core Data stack
101+
102+
- (NSManagedObjectContext *) managedObjectContext {
103+
104+
if (managedObjectContext != nil) {
105+
return managedObjectContext;
106+
}
107+
108+
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
109+
if (coordinator != nil) {
110+
managedObjectContext = [[NSManagedObjectContext alloc] init];
111+
[managedObjectContext setPersistentStoreCoordinator: coordinator];
112+
}
113+
return managedObjectContext;
114+
}
115+
116+
117+
- (NSManagedObjectModel *)managedObjectModel {
118+
119+
if (managedObjectModel != nil) {
120+
return managedObjectModel;
121+
}
122+
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
123+
return managedObjectModel;
124+
}
125+
126+
127+
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
128+
129+
if (persistentStoreCoordinator != nil) {
130+
return persistentStoreCoordinator;
131+
}
132+
133+
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DateSectionTitles.sqlite"]];
134+
135+
136+
BOOL firstRun = NO;
137+
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeUrl path] isDirectory:NULL]) {
138+
firstRun = YES;
139+
}
140+
141+
NSError *error = nil;
142+
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
143+
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
144+
/*
145+
Replace this implementation with code to handle the error appropriately.
146+
147+
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
148+
*/
149+
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
150+
abort();
151+
}
152+
153+
/*
154+
If this is the first run, populate a new store with events whose timestamps are spaced every 7 days throughout 2009.
155+
*/
156+
if (firstRun) {
157+
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
158+
[context setPersistentStoreCoordinator:persistentStoreCoordinator];
159+
160+
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
161+
[dateComponents setYear:2009];
162+
163+
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
164+
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
165+
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
166+
167+
NSCalendar *calendar = [NSCalendar currentCalendar];
168+
NSInteger day;
169+
170+
for (day = 1; day < 365; day += 7) {
171+
172+
[dateComponents setDay:day];
173+
NSDate *date = [calendar dateFromComponents:dateComponents];
174+
175+
Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
176+
newEvent.timeStamp = date;
177+
newEvent.title = [NSString stringWithFormat:@"%@ (day %d)", [dateFormatter stringFromDate:date], [dateComponents day]];
178+
}
179+
180+
[context save:NULL];
181+
182+
[context release];
183+
[dateComponents release];
184+
}
185+
186+
return persistentStoreCoordinator;
187+
}
188+
189+
190+
#pragma mark -
191+
#pragma mark Application's Documents directory
192+
193+
- (NSString *)applicationDocumentsDirectory {
194+
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
195+
}
196+
197+
198+
#pragma mark -
199+
#pragma mark Memory management
200+
201+
- (void)dealloc {
202+
203+
[managedObjectContext release];
204+
[managedObjectModel release];
205+
[persistentStoreCoordinator release];
206+
207+
[navigationController release];
208+
[window release];
209+
[super dealloc];
210+
}
211+
212+
213+
@end
214+

DateSectionTitles/Classes/Event.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
/*
3+
File: Event.h
4+
Abstract: Custom class for the Event entity.
5+
6+
The timeStamp and title are persistent attributes; the sectionIdentifier is transient and derived from timeStamp.
7+
* timeStamp is the date on which the event occurred.
8+
* title is displayed in the table view rows.
9+
When the default data is created in the application delegate, the title is initialized to a string representation of the date.
10+
* sectionIdentifier is used to divide the events into sections in the table view.
11+
sectionIdentifier is a string value representing the number ((year * 1000) + month). Using this value, events can be correctly ordered and grouped regardless of the actual name of the month. It is calculated and cached on demand in the custom accessor method.
12+
13+
Version: 1.2
14+
15+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
16+
Inc. ("Apple") in consideration of your agreement to the following
17+
terms, and your use, installation, modification or redistribution of
18+
this Apple software constitutes acceptance of these terms. If you do
19+
not agree with these terms, please do not use, install, modify or
20+
redistribute this Apple software.
21+
22+
In consideration of your agreement to abide by the following terms, and
23+
subject to these terms, Apple grants you a personal, non-exclusive
24+
license, under Apple's copyrights in this original Apple software (the
25+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
26+
Software, with or without modifications, in source and/or binary forms;
27+
provided that if you redistribute the Apple Software in its entirety and
28+
without modifications, you must retain this notice and the following
29+
text and disclaimers in all such redistributions of the Apple Software.
30+
Neither the name, trademarks, service marks or logos of Apple Inc. may
31+
be used to endorse or promote products derived from the Apple Software
32+
without specific prior written permission from Apple. Except as
33+
expressly stated in this notice, no other rights or licenses, express or
34+
implied, are granted by Apple herein, including but not limited to any
35+
patent rights that may be infringed by your derivative works or by other
36+
works in which the Apple Software may be incorporated.
37+
38+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
39+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
40+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
41+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
42+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
43+
44+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
45+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
48+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
49+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
50+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
51+
POSSIBILITY OF SUCH DAMAGE.
52+
53+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
54+
55+
*/
56+
57+
#import <CoreData/CoreData.h>
58+
59+
60+
@interface Event : NSManagedObject {
61+
}
62+
63+
@property (nonatomic, retain) NSString *title;
64+
65+
@property (nonatomic, retain) NSDate *timeStamp;
66+
@property (nonatomic, retain) NSDate *primitiveTimeStamp;
67+
68+
@property (nonatomic, retain) NSString *sectionIdentifier;
69+
@property (nonatomic, retain) NSString *primitiveSectionIdentifier;
70+
71+
@end
72+
73+
74+

0 commit comments

Comments
 (0)