-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBPCoreDataDrivenTableViewController.m
More file actions
320 lines (244 loc) · 11.3 KB
/
BPCoreDataDrivenTableViewController.m
File metadata and controls
320 lines (244 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// BPCoreDataDrivenTableViewController.m
//
// Created by Jon Olson on 2/9/10.
// Copyright 2010 Ballistic Pigeon, LLC. All rights reserved.
//
#import "BPCoreDataDrivenTableViewController.h"
@interface BPCoreDataDrivenTableViewController (Private)
@end
@implementation BPCoreDataDrivenTableViewController
#pragma mark -
#pragma mark Construction and deallocation
- (id)init {
if (self = [super initWithStyle:UITableViewStylePlain]) {
}
return self;
}
- (void)dealloc {
[entityDescription release];
[fetchedResultsController release];
[managedObjectContext release];
[super dealloc];
}
#pragma mark -
#pragma mark Abstract methods that subclasses should implement to ensure sane behavior
- (NSString *)managedEntityName {
return nil;
}
- (NSString *)defaultSortKey {
return nil;
}
- (NSString *)defaultSectionKey {
return nil;
}
- (NSString *)defaultCellLabelKey {
return nil;
}
- (NSString *)defaultCacheName {
return nil;
}
- (NSString *)inspectorClassName {
return nil;
}
- (void)createFetchedResultsController {
if ([self entityDescription]) {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:[self entityDescription]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self defaultSortKey] ascending:YES];
if ([self defaultSectionKey]) {
NSSortDescriptor *sectionSort = [[NSSortDescriptor alloc] initWithKey:[self defaultSectionKey] ascending:YES];
[fetchRequest setSortDescriptors:A(sectionSort, sortDescriptor)];
}
else
[fetchRequest setSortDescriptors:A(sortDescriptor)];
self.fetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:[self defaultCacheName]] autorelease];
}
return;
}
#pragma mark -
#pragma mark Accessors
@synthesize managedObjectContext, fetchedResultsController, entityDescription;
- (NSFetchedResultsController *)fetchedResultsController {
if (!fetchedResultsController) {
[self createFetchedResultsController];
[fetchedResultsController setDelegate:self];
}
return fetchedResultsController;
}
- (NSEntityDescription *)entityDescription {
if (!entityDescription && self.managedObjectContext && [self managedEntityName]) {
NSManagedObjectModel *model = [[self.managedObjectContext persistentStoreCoordinator] managedObjectModel];
entityDescription = [[[model entitiesByName] objectForKey:[self managedEntityName]] retain];
}
return entityDescription;
}
- (Class)inspectorClass {
if (!inspectorClass && [self inspectorClassName])
inspectorClass = NSClassFromString([self inspectorClassName]);
return inspectorClass;
}
#pragma mark -
#pragma mark View loading and unloading
- (void)viewDidLoad {
[super viewDidLoad];
if ([self managedEntityName] && [self inspectorClassName] && !self.navigationItem.rightBarButtonItem)
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEntity:)] autorelease];
}
#pragma mark -
#pragma mark View appearing and disappeaing
- (void)viewWillAppear:(BOOL)animated {
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
[[[UIAlertView alloc] initWithTitle:@"Error Fetching Results" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}
#pragma mark -
#pragma mark Rotation is cool with ushort
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#pragma mark -
#pragma mark UITableViewDelegate and UITableViewDataSource implementation
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [self cellForManagedObject:managedObject inTableView:tableView];
[self configureCell:cell withManagedObject:managedObject];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.fetchedResultsController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self inspectorClassName])
return indexPath;
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController <BPManagedObjectInspectorViewController> *viewController = [[[self inspectorClass] alloc] init];
[viewController setManagedObjectContext:self.managedObjectContext];
[viewController setManagedObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
[managedObjectContext deleteObject:object];
NSError *error = nil;
if (![managedObjectContext save:&error])
[[[[UIAlertView alloc] initWithTitle:@"Delete Failed" message:[error localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
}
}
#pragma mark -
#pragma mark NSFetchedResultsControllerDelegate implementation
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
if ([tableView cellForRowAtIndexPath:indexPath]) {
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
withManagedObject:[controller objectAtIndexPath:indexPath]];
[tableView reloadRowsAtIndexPaths:A(indexPath) withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
#pragma mark -
#pragma mark Default cell creation
- (UITableViewCell *)cellForManagedObject:(NSManagedObject *)object inTableView:(UITableView *)tableView {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell withManagedObject:(NSManagedObject *)object {
NSString *cellLabelKey = [self defaultCellLabelKey];
NSAssert(cellLabelKey, @"Must implement -defaultCellLabelKey to use default cell configruation");
cell.textLabel.text = [object valueForKey:cellLabelKey];
}
#pragma mark -
#pragma mark Optional method to allow subclasses to prepare new objects
- (void)prepareNewManagedObject:(NSManagedObject *)managedObject {
// Stub
}
#pragma mark -
#pragma mark UI Actions
- (IBAction)addEntity:(id)sender {
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[self managedEntityName] inManagedObjectContext:self.managedObjectContext];
[self prepareNewManagedObject:newObject];
UIViewController <BPManagedObjectInspectorViewController> *viewController = (UIViewController <BPManagedObjectInspectorViewController> *)[[[self inspectorClass] alloc] init];
[viewController setManagedObjectContext:self.managedObjectContext];
[viewController setManagedObject:newObject];
[viewController setEditing:YES];
viewController.navigationItem.hidesBackButton = YES;
viewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAdd:)] autorelease];
viewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:viewController action:@selector(create:)] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
- (void)cancelAdd:(id)sender {
UIViewController <BPManagedObjectInspectorViewController> *viewController = (UIViewController <BPManagedObjectInspectorViewController> *)[self.navigationController topViewController];
NSManagedObject *managedObject = [viewController managedObject];
if ([managedObject isInserted]) {
[self.managedObjectContext deleteObject:managedObject];
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
[[[[UIAlertView alloc] initWithTitle:@"Error Canceling Insert" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
}
else
[self.navigationController popToViewController:self animated:YES];
}
else
[self.navigationController popToViewController:self animated:YES];
}
@end