forked from danielctull/DataCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCTManagedObjectRelationshipsViewController.m
108 lines (78 loc) · 3.18 KB
/
DCTManagedObjectRelationshipsViewController.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
105
106
107
108
//
// DCTManagedObjectRelationshipsViewController.m
// DCTCoreDataBrowser
//
// Created by Daniel Tull on 23.02.2011.
// Copyright 2011 Daniel Tull. All rights reserved.
//
#import "DCTManagedObjectRelationshipsViewController.h"
#import "DCTManagedObjectViewController.h"
#import "NSManagedObject+DCTNiceDescription.h"
@interface DCTManagedObjectRelationshipsViewController ()
- (void)setupRelatedObjects;
@end
@implementation DCTManagedObjectRelationshipsViewController
@synthesize managedObject, relationship;
- (id)init {
return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)dealloc {
[relatedObjects release], relatedObjects = nil;
[managedObject release], managedObject = nil;
[relationship release], relationship = nil;
[super dealloc];
}
- (void)setManagedObject:(NSManagedObject *)mo {
NSManagedObject *oldManagedObject = managedObject;
managedObject = [mo retain];
[oldManagedObject release];
[self setupRelatedObjects];
}
- (void)setupRelatedObjects {
if (!(self.managedObject) || !(self.relationship)) return;
NSSet *ro = [self.managedObject valueForKey:[relationship name]];
[relatedObjects release];
relatedObjects = [[ro allObjects] retain];
}
- (void)setRelationship:(NSRelationshipDescription *)r {
NSRelationshipDescription *old = relationship;
relationship = [r retain];
[old release];
self.title = [relationship name];
[self setupRelatedObjects];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [relatedObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!(cell))
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [[relatedObjects objectAtIndex:indexPath.row] dct_niceDescription];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObject *object = [relatedObjects objectAtIndex:indexPath.row];
[object.managedObjectContext deleteObject:object];
[object.managedObjectContext save:NULL];
NSMutableArray *objects = [relatedObjects mutableCopy];
[objects removeObjectAtIndex:indexPath.row];
[relatedObjects release]; relatedObjects = objects;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] init];
vc.managedObject = [relatedObjects objectAtIndex:indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
@end