Skip to content

Instantly share code, notes, and snippets.

@keicoder
Last active August 29, 2015 13:56
Show Gist options
  • Save keicoder/8912254 to your computer and use it in GitHub Desktop.
Save keicoder/8912254 to your computer and use it in GitHub Desktop.
objective-c : make basic table view with custom background and jason data v.1
//make basic table view with custom background and jason data
//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, nonatomic) NSArray* sandwiches;
@end
//AppDelegate.m
@implementation AppDelegate
{
NSArray* _sandwiches;
}
#define debug 1
- (NSArray *)sandwiches {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
return _sandwiches;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[self loadSandwiches];
return YES;
}
- (void)loadSandwiches {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
NSString* path = [[NSBundle mainBundle] pathForResource: @"Sandwiches"
ofType: @"json"];
NSString* data = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: nil];
NSData* resultData = [data dataUsingEncoding:NSUTF8StringEncoding];
_sandwiches = [NSJSONSerialization JSONObjectWithData:resultData
options:kNilOptions
error:nil];
}
@end
//SanwichesViewController.h
@interface SandwichesViewController : UITableViewController
@end
//SanwichesViewController.m
#import "SanwichesViewController.h"
#import "SandwichViewController.h"
#import "AppDelegate.h"
@interface SandwichesViewController ()
@end
@implementation SandwichesViewController
#define debug 1
- (NSArray*) sandwiches
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return appDelegate.sandwiches; //sandwiches 배열
}
- (void)viewDidLoad {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[super viewDidLoad];
//헤더 뷰 및 백그라운드 뷰 설정
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 350)];
headerView.backgroundColor = [UIColor clearColor];
UIImageView* header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Sarnie"]];
header.center = CGPointMake(220, 190);
[headerView addSubview:header];
self.tableView.tableHeaderView = headerView;
[self.tableView setBackgroundView:[[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"Background-LowerLayer"]]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
return [self sandwiches].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.5f];
NSDictionary* sandwich = (NSDictionary*)[self sandwiches][indexPath.row];
cell.textLabel.text = sandwich[@"title"];
return cell;
}
@end
//SandwichViewController.h
@interface SandwichViewController : UIViewController
@end
//SandwichViewController.m
@interface SandwichViewController ()
@property (weak, nonatomic) IBOutlet UIBarButtonItem *closeButton;
@end
@implementation SandwichViewController
#define debug 1
- (void)viewDidLoad
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[super viewDidLoad];
}
#pragma mark - 버튼 액션 메소드
- (IBAction)closeButtonTapped:(id)sender {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment