Skip to content

Instantly share code, notes, and snippets.

@evandavey
Created March 17, 2012 17:43
Show Gist options
  • Save evandavey/2063333 to your computer and use it in GitHub Desktop.
Save evandavey/2063333 to your computer and use it in GitHub Desktop.
RecipeTableViewController
//
// RKGHIssuesTableViewController.m
// RKGithub
//
// Created by Blake Watters on 2/22/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
#import <RestKit/RestKit.h>
#import <RestKit/UI.h>
#import "Recipe.h"
#import "CDRecipeMonkeyRecipesTableViewController.h"
#import "CDRecipeMonkeyRecipeViewController.h"
#import "RKGHLoadingView.h"
@interface CDRecipeMonkeyRecipesTableViewController ()
@property (nonatomic, strong) RKFetchedResultsTableController *tableController;
@end
@implementation CDRecipeMonkeyRecipesTableViewController
@synthesize tableController;
- (void)viewDidLoad
{
[super viewDidLoad];
/**
Configure the RestKit table controller to drive our view
*/
self.tableController = [[RKObjectManager sharedManager] fetchedResultsTableControllerForTableViewController:self];
self.tableController.autoRefreshFromNetwork = YES;
self.tableController.pullToRefreshEnabled = YES;
self.tableController.resourcePath = @"/recipe";
self.tableController.variableHeightRows = YES;
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
self.tableController.sortDescriptors = [NSArray arrayWithObject:descriptor];
/**
Configure the Pull to Refresh View
*/
NSBundle *restKitResources = [NSBundle restKitResourcesBundle];
UIImage *arrowImage = [restKitResources imageWithContentsOfResource:@"blueArrow" withExtension:@"png"];
[[RKRefreshTriggerView appearance] setTitleFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13]];
[[RKRefreshTriggerView appearance] setLastUpdatedFont:[UIFont fontWithName:@"HelveticaNeue" size:11]];
[[RKRefreshTriggerView appearance] setArrowImage:arrowImage];
/**
Configure a basic loading view
*/
RKGHLoadingView *loadingView = [[RKGHLoadingView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
loadingView.center = self.tableView.center;
self.tableController.loadingView = loadingView;
/**
Setup some images for various table states
*/
self.tableController.imageForOffline = [UIImage imageNamed:@"offline.png"];
self.tableController.imageForError = [UIImage imageNamed:@"error.png"];
self.tableController.imageForEmpty = [UIImage imageNamed:@"empty.png"];
/**
Configure our RKGHIssue -> UITableViewCell mappings. When RestKit loads the
remote resource collection, the JSON payload will be object mapped into local
RKGHIssue instances on a background thread. Once the payload has been processed,
the table controller will object map the RKGHIssue instances into table cells and
render the tableView.
*/
RKTableViewCellMapping *cellMapping = [RKTableViewCellMapping cellMapping];
cellMapping.cellClassName = @"CDRecipeCell";
cellMapping.reuseIdentifier = @"CDRecipe";
cellMapping.rowHeight = 100.0;
[cellMapping mapKeyPath:@"name" toAttribute:@"titleLabel.text"];
[cellMapping mapKeyPath:@"source.name" toAttribute:@"descriptionLabel.text"];
[cellMapping mapKeyPath:@"name" toAttribute:@"recipeName"];
[tableController mapObjectsWithClass:[Recipe class] toTableCellsWithMapping:cellMapping];
/*
Use a custom Nib to draw our table cells for RKGHIssue objects
*/
[self.tableView registerNib:[UINib nibWithNibName:@"CDRecipeCell" bundle:nil] forCellReuseIdentifier:@"CDRecipe"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
/**
Load the table view!
*/
[tableController loadTable];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Recipe *recipe = [self.tableController objectForRowAtIndexPath:indexPath];
NSLog(@"Recipe selected is %@",recipe);
if ([segue.destinationViewController respondsToSelector:@selector(setDetailItem:)]) {
// use performSelector:withObject: to send without compiler checking
// (which is acceptable here because we used introspection to be sure this is okay)
[segue.destinationViewController performSelector:@selector(setDetailItem:) withObject:recipe];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment