Files
RestKit/Examples/RKTableViewExample/RKTableViewExample/MasterViewController.m
Brian Morton b44c4974ee Fix RKTableViewExample.
* Add necessary frameworks and header configuration.
* Update calls to deprecated methods.
2012-02-20 23:52:09 -08:00

373 lines
16 KiB
Objective-C

//
// MasterViewController.m
// RKTableViewExample
//
// Created by Blake Watters on 8/2/11.
// Copyright 2011 RestKit. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "Contact.h"
@interface MasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation MasterViewController
@synthesize tableController = __tableController;
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize segmentedControl = __segementedControl;
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];
}
return self;
}
#pragma mark - View lifecycle
- (void)loadStaticTable {
// // Put some data in the table
// Contact* blake = [Contact new];
// blake.firstName = @"Blake";
// blake.lastName = @"Watters";
// blake.emailAddress = @"blake@gateguruapp.com";
//
// Contact* jeff = [Contact new];
// jeff.firstName = @"Jeff";
// jeff.lastName = @"Arena";
// jeff.emailAddress = @"jeff@gateguruapp.com";
//
// Contact* dan = [Contact new];
// dan.firstName = @"Dan";
// dan.lastName = @"Gellert";
// dan.emailAddress = @"dan@gateguruapp.com";
//
// NSArray* contacts = [NSArray arrayWithObjects:blake, jeff, dan, nil];
// [__tableController loadObjects:contacts];
NSArray* tableItems = [RKTableItem tableItemsFromStrings:@"User", @"Connect", @"Bookmarks", @"Reviews & Tips", @"Scores", nil];
[__tableController loadTableItems:tableItems
withMappingBlock:^(RKTableViewCellMapping* cellMapping) {
cellMapping.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cellMapping.onSelectCellForObjectAtIndexPath = ^(UITableViewCell *cell, id object, NSIndexPath* indexPath) {
RKTableItem* tableItem = (RKTableItem*) object;
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Alert!"
message:tableItem.text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
};
}];
}
- (void)loadNetworkTable {
[__tableController loadTableFromResourcePath:@"/contacts.json"];
}
- (void)loadCoreDataTable {
}
- (void)segmentedControlDidChangeValue {
switch (__segementedControl.selectedSegmentIndex) {
case 0:
__tableController.pullToRefreshEnabled = NO;
[self loadStaticTable];
break;
case 1:
__tableController.pullToRefreshEnabled = YES;
[self loadNetworkTable];
break;
case 2:
__tableController.pullToRefreshEnabled = YES;
[self loadCoreDataTable];
break;
default:
NSLog(@"Unknown index %d selected for __segmentedControl: %@", __segementedControl.selectedSegmentIndex, __segementedControl);
break;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
// Configure RestKit Logging
RKLogConfigureByName("RestKit/UI", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network*", RKLogLevelDebug);
// Configure the object manager
RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:[NSURL URLWithString:@"http://localhost:4567/"]];
[manager.mappingProvider setMapping:[RKObjectMapping mappingForClass:[Contact class] usingBlock:^(RKObjectMapping* mapping) {
[mapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
[mapping mapKeyPath:@"last_name" toAttribute:@"lastName"];
[mapping mapKeyPath:@"email_address" toAttribute:@"emailAddress"];
}] forKeyPath:@"contacts"];
__tableController = [RKTableController tableControllerForTableViewController:self];
[__tableController mapObjectsWithClass:[Contact class]
toTableCellsWithMapping:[RKTableViewCellMapping cellMappingUsingBlock:^(RKTableViewCellMapping* cellMapping) {
cellMapping.style = UITableViewCellStyleSubtitle;
cellMapping.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cellMapping mapKeyPath:@"fullName" toAttribute:@"textLabel.text"];
[cellMapping mapKeyPath:@"emailAddress" toAttribute:@"detailTextLabel.text"];
cellMapping.onSelectCellForObjectAtIndexPath = ^(UITableViewCell *cell, id object, NSIndexPath* indexPath) {
Contact *contact = object;
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Cell Selected!"
message:[NSString stringWithFormat:@"You selected '%@'", contact.fullName]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
};
// TODO: configureCell:forObject:atIndexPath:
}]];
[self.segmentedControl addTarget:self action:@selector(segmentedControlDidChangeValue) forControlEvents:UIControlEventValueChanged];
[self loadStaticTable];
// __tableController.onAddCell = ^(UITableViewCell* cell) {
//
// };
// __tableController.delegate = self;
// [__tableController addSection:[RKTableViewSection sectionWithBlock:^(RKTableViewSection* section) {
// section.headerTitle = @"Account";
// UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RKTableViewCell"];
// cell.textLabel.text = @"This is a cell";
// [section addCell:cell];
// UITableViewCell* cell2 = [UITableViewCell new];
// cell2.textLabel.text = @"This is another cell";
// [section addCell:cell2];
// }]];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// The table view should not be re-orderable.
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailViewController.detailItem = selectedObject;
[self.navigationController pushViewController:detailViewController animated:YES];
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
- (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:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
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];
}
/*
// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
// In the simplest, most efficient, case, reload the table view.
[self.tableView reloadData];
}
*/
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[managedObject valueForKey:@"timeStamp"] description];
}
- (void)insertNewObject
{
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
@end