Created
February 10, 2014 09:42
-
-
Save keicoder/8913067 to your computer and use it in GitHub Desktop.
objective-c : make basic table view with custom background and jason data v.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//make basic table view with custom background and jason data v.2 | |
//make SandwichView (Detail View) with data | |
//CollectionViewLabelCell.h | |
@interface CollectionViewLabelCell : UICollectionViewCell | |
@property (nonatomic, readonly) UILabel* title; | |
@end | |
//CollectionViewLabelCell.m | |
#import <QuartzCore/QuartzCore.h> | |
@implementation CollectionViewLabelCell | |
{ | |
UILabel* _title; | |
} | |
#define debug 1 | |
#pragma mark - 타이틀 label | |
- (UILabel *)title { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
return _title; | |
} | |
#pragma mark - add a label to the cell | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
self = [super initWithFrame:frame]; | |
if (self) { | |
//add a label to the cell | |
//CGRectInset : Returns a rectangle that is smaller or larger than the source rectangle | |
//CGRect, x-coordinate, y-coordinate | |
_title = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 3.0, 3.0)]; | |
_title.textAlignment = NSTextAlignmentCenter; | |
_title.font = [UIFont systemFontOfSize:12.0f]; | |
[self.contentView addSubview:_title]; | |
// make it a rounded rectangle | |
self.layer.cornerRadius = 5; | |
self.layer.masksToBounds = YES; | |
self.layer.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor; | |
} | |
return self; | |
} | |
@end | |
--- | |
//SandwichViewController.h | |
@interface SandwichViewController : UIViewController | |
@property (nonatomic, strong) NSDictionary* sandwich; | |
@end | |
//SandwichViewController.m | |
#import "CollectionViewLabelCell.h" //Collection View 레이블 | |
@interface SandwichViewController () <UICollectionViewDataSource> //Collection View 데이터소스 델리게이트 | |
//뷰 property | |
@property (weak, nonatomic) IBOutlet UIImageView *imageView; | |
@property (weak, nonatomic) IBOutlet UICollectionView *keywordCollectionView; | |
@property (weak, nonatomic) IBOutlet UITextView *instructionTextView; | |
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar; | |
@property (weak, nonatomic) IBOutlet UIBarButtonItem *closeButton; | |
@end | |
@implementation SandwichViewController | |
{ | |
NSDictionary* _sandwich; | |
} | |
#define debug 1 | |
- (NSDictionary *)sandwich { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
return _sandwich; | |
} | |
- (void)setSandwich:(NSDictionary *)sandwich { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
_sandwich = sandwich; | |
[self updateControlsWithYummySandwichData]; | |
} | |
#pragma mark - 뷰 라이프 사이클 | |
- (void)viewDidLoad | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[super viewDidLoad]; | |
UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background-LowerLayer"]]; | |
background.alpha = 0.5f; | |
[self.view addSubview:background]; | |
[self.view sendSubviewToBack:background]; | |
// configure the collection view | |
[self.keywordCollectionView registerClass:[CollectionViewLabelCell class] forCellWithReuseIdentifier:@"cell"]; | |
self.keywordCollectionView.dataSource = self; | |
self.keywordCollectionView.backgroundColor = [UIColor clearColor]; | |
UICollectionViewFlowLayout* flowLayout = (UICollectionViewFlowLayout*)self.keywordCollectionView.collectionViewLayout; | |
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; | |
flowLayout.itemSize = CGSizeMake(120, 20); | |
[self updateControlsWithYummySandwichData]; | |
} | |
#pragma mark - set title, instructions and image from the sandwich | |
- (void) updateControlsWithYummySandwichData { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
// set the title, instructions and image from the sandwich | |
NSString* imageName = self.sandwich[@"image"]; | |
UIImage* image = [UIImage imageNamed:imageName]; | |
self.imageView.image = image; | |
self.navigationBar.topItem.title = self.sandwich[@"title"]; | |
NSArray* instructions = self.sandwich[@"instructions"]; | |
self.instructionTextView.text = [instructions componentsJoinedByString:@"\n\n"]; | |
} | |
#pragma mark - Collection View 데이터소스 메소드 | |
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
NSArray* ingredients = _sandwich[@"keywords"]; | |
return ingredients.count; | |
} | |
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView | |
cellForItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
CollectionViewLabelCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; | |
NSArray* ingredients = _sandwich[@"keywords"]; | |
cell.title.text = ingredients[indexPath.row]; | |
return cell; | |
} | |
#pragma mark - 버튼 액션 메소드 | |
- (IBAction)closeButtonTapped:(id)sender { | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
@end | |
--- | |
//SanwichesViewController.m | |
#pragma mark - Navigation | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
SandwichViewController* sandwichVC = (SandwichViewController*)segue.destinationViewController; | |
NSIndexPath *path = [self.tableView indexPathForSelectedRow]; | |
sandwichVC.sandwich = [self sandwiches][path.row]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment