Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
Improve API of managed object view controller
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Aug 18, 2012
1 parent 1d9823f commit 623cdc0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
3 changes: 2 additions & 1 deletion DataCenter/DCTManagedObjectViewController.h
Expand Up @@ -11,6 +11,7 @@

@interface DCTManagedObjectViewController : UITableViewController

@property (nonatomic, strong) NSManagedObject *managedObject;
- (id)initWithManagedObject:(NSManagedObject *)managedObject;
@property (nonatomic, strong, readonly) NSManagedObject *managedObject;

@end
56 changes: 23 additions & 33 deletions DataCenter/DCTManagedObjectViewController.m
Expand Up @@ -17,36 +17,26 @@ @interface DCTManagedObjectViewController ()
@end

@implementation DCTManagedObjectViewController {
__strong NSArray *relationships;
__strong NSArray *attributes;
__strong NSArray *_relationships;
__strong NSArray *_attributes;
}

@synthesize managedObject;

#pragma mark - NSObject

- (id)init {
return [self initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - DCTManagedObjectViewController

- (void)setManagedObject:(NSManagedObject *)mo {
managedObject = mo;

NSEntityDescription *entity = [managedObject entity];
- (id)initWithManagedObject:(NSManagedObject *)managedObject {

self.title = [entity name];
self = [self initWithStyle:UITableViewStyleGrouped];
if (!self) return nil;

relationships = [[entity relationshipsByName] allKeys];
_managedObject = managedObject;

attributes = [[entity attributesByName] allKeys];
NSEntityDescription *entity = [managedObject entity];
self.title = [entity name];
_relationships = [[entity relationshipsByName] allKeys];
_attributes = [[entity attributesByName] allKeys];

if ([self isViewLoaded])
[self.tableView reloadData];
return self;
}

#pragma mark - Table view data source
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
Expand All @@ -55,10 +45,10 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section == DCTManagedObjectViewControllerAttributeSection)
return [attributes count];
return [_attributes count];

if (section == DCTManagedObjectViewControllerRelationshipSection)
return [relationships count];
return [_relationships count];

return 0;
}
Expand Down Expand Up @@ -88,18 +78,18 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerAttributeSection) {

NSString *attributeName = [attributes objectAtIndex:indexPath.row];
NSString *attributeName = [_attributes objectAtIndex:indexPath.row];

cell.textLabel.text = attributeName;
cell.detailTextLabel.text = [[managedObject valueForKey:attributeName] description];
cell.detailTextLabel.text = [[self.managedObject valueForKey:attributeName] description];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
}

if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerRelationshipSection) {

NSString *relationshipName = [relationships objectAtIndex:indexPath.row];
NSString *relationshipName = [_relationships objectAtIndex:indexPath.row];

cell.textLabel.text = relationshipName;

Expand All @@ -108,28 +98,28 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if ([relationship isToMany])
cell.detailTextLabel.text = [NSString stringWithFormat:@"Many %@s", [[relationship destinationEntity] name]];
else
cell.detailTextLabel.text = [[managedObject valueForKey:relationshipName] dct_niceDescription];
cell.detailTextLabel.text = [[self.managedObject valueForKey:relationshipName] dct_niceDescription];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerRelationshipSection) {

NSString *relationshipName = [relationships objectAtIndex:indexPath.row];
NSString *relationshipName = [_relationships objectAtIndex:indexPath.row];

NSRelationshipDescription *relationship = [[[self.managedObject entity] relationshipsByName] objectForKey:relationshipName];

if (![relationship isToMany]) {

NSManagedObject *mo = [managedObject valueForKey:relationshipName];

DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] init];
vc.managedObject = mo;
NSManagedObject *managedObject = [self.managedObject valueForKey:relationshipName];
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] initWithManagedObject:managedObject];
[self.navigationController pushViewController:vc animated:YES];

} else {
Expand Down
9 changes: 4 additions & 5 deletions DataCenter/_DCTManagedObjectContextViewController.m
Expand Up @@ -63,9 +63,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

NSEntityDescription *entity = [_entities objectAtIndex:indexPath.section];
NSArray *objects = [_fetchedEntities objectForKey:[entity name]];
NSManagedObject *mo = [objects objectAtIndex:indexPath.row];
NSManagedObject *managedObject = [objects objectAtIndex:indexPath.row];

cell.textLabel.text = [mo dct_niceDescription];
cell.textLabel.text = [managedObject dct_niceDescription];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
Expand All @@ -77,10 +77,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

NSEntityDescription *entity = [_entities objectAtIndex:indexPath.section];
NSArray *objects = [_fetchedEntities objectForKey:[entity name]];
NSManagedObject *mo = [objects objectAtIndex:indexPath.row];
NSManagedObject *managedObject = [objects objectAtIndex:indexPath.row];

DCTManagedObjectViewController *vc = [DCTManagedObjectViewController new];
vc.managedObject = mo;
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] initWithManagedObject:managedObject];
[self.navigationController pushViewController:vc animated:YES];
}

Expand Down
4 changes: 2 additions & 2 deletions DataCenter/_DCTManagedObjectRelationshipsViewController.m
Expand Up @@ -54,8 +54,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] init];
vc.managedObject = [_relatedObjects objectAtIndex:indexPath.row];
NSManagedObject *managedObject = [_relatedObjects objectAtIndex:indexPath.row];
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] initWithManagedObject:managedObject];
[self.navigationController pushViewController:vc animated:YES];
}

Expand Down

0 comments on commit 623cdc0

Please sign in to comment.