Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created February 12, 2014 03:30
Show Gist options
  • Save keicoder/8949627 to your computer and use it in GitHub Desktop.
Save keicoder/8949627 to your computer and use it in GitHub Desktop.
objective-c : make basic table view with custom background and jason data v.3
//make basic table view with custom background and jason data v.3
//iterate over the recipes and use the addRecipeAtOffset: forSandwich: method
//to add each recipe to the view
//DynamicSandwichViewController.h
@interface DynamicSandwichViewController : UIViewController
@end
//DynamicSandwichViewController.m
#import "DynamicSandwichViewController.h"
#import "SandwichViewController.h"
#import "AppDelegate.h"
@interface DynamicSandwichViewController ()
@end
@implementation DynamicSandwichViewController
{
NSMutableArray* _views; //use this variable to keep track of the added views
}
#define debug 1
#pragma mark - retrieve sandwiches from the app delegate
- (NSArray*)sandwiches {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
return appDelegate.sandwiches;
}
#pragma mark - addRecipeAtOffset (add each recipe to the view)
- (UIView*)addRecipeAtOffset:(CGFloat)offset forSandwich:(NSDictionary*)sandwich {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
//CGRectOffset : rect, offset x, y (positive numbers are to the right and up)
CGRect frameForView = CGRectOffset(self.view.bounds, 0.0, self.view.bounds.size.height - offset);
// 1. create the view controller
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SandwichViewController* viewController = [mystoryboard instantiateViewControllerWithIdentifier:@"SandwichVC"];
// 2. set the frame and provide some data
UIView* view = viewController.view;
view.frame = frameForView;
viewController.sandwich = sandwich;
// 3. add as a child
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
return view;
}
#pragma mark - 뷰 라이프 사이클
- (void)viewDidLoad
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[super viewDidLoad];
// Background image
UIImageView* backgroundImageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Background-LowerLayer.png"]];
[self.view addSubview:backgroundImageView];
// Header logo
UIImageView* header = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Sarnie.png"]];
header.center = CGPointMake(220, 190);
[self.view addSubview:header];
//iterate over the recipes and use the addRecipeAtOffset: forSandwich: method
//to add each recipe to the view.
_views = [NSMutableArray new];
CGFloat offset = 250.0f;
for (NSDictionary* sandwich in [self sandwiches]) {
[_views addObject:[self addRecipeAtOffset:offset forSandwich:sandwich]];
offset -= 50.0f; }
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment