forked from danielctull/DataCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCTManagedObjectContextViewController.m
104 lines (73 loc) · 3.15 KB
/
DCTManagedObjectContextViewController.m
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
//
// DCTManagedObjectContextViewController.m
// DCTCoreDataBrowser
//
// Created by Daniel Tull on 23.02.2011.
// Copyright 2011 Daniel Tull. All rights reserved.
//
#import "DCTManagedObjectContextViewController.h"
#import "DCTManagedObjectViewController.h"
#import "NSManagedObject+DCTNiceDescription.h"
@implementation DCTManagedObjectContextViewController
@synthesize managedObjectContext;
#pragma mark - NSObject
- (id)init {
if (!(self = [self initWithStyle:UITableViewStyleGrouped])) return nil;
self.title = @"Data Center";
return self;
}
- (void)dealloc {
[managedObjectContext release], managedObjectContext = nil;
[super dealloc];
}
#pragma mark - DCTManagedObjectContextViewController
- (void)setManagedObjectContext:(NSManagedObjectContext *)moc {
NSManagedObjectContext *old = managedObjectContext;
managedObjectContext = [moc retain];
[old release];
[entities release];
entities = [[[[self.managedObjectContext persistentStoreCoordinator] managedObjectModel] entities] retain];
[fetchedEntities release];
fetchedEntities = [[NSMutableDictionary alloc] initWithCapacity:[entities count]];
for (NSEntityDescription *entity in entities) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSArray *fetchResult = [managedObjectContext executeFetchRequest:request error:NULL];
[request release];
[fetchedEntities setObject:fetchResult forKey:[entity name]];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [entities count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[entities objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[fetchedEntities objectForKey:[[entities objectAtIndex:section] name]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSEntityDescription *entity = [entities objectAtIndex:indexPath.section];
NSArray *objects = [fetchedEntities objectForKey:[entity name]];
NSManagedObject *mo = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = [mo dct_niceDescription];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSEntityDescription *entity = [entities objectAtIndex:indexPath.section];
NSArray *objects = [fetchedEntities objectForKey:[entity name]];
NSManagedObject *mo = [objects objectAtIndex:indexPath.row];
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] init];
vc.managedObject = mo;
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
@end