Created
March 10, 2014 08:15
-
-
Save keicoder/9461229 to your computer and use it in GitHub Desktop.
objective-c : making basic UICollectionView
This file contains hidden or 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
//making basic UICollectionView | |
//UICollectionView 주요 메소드 (UITableView와 비교) | |
//UICollectionView vs UITableView | |
//Item(Cell) 갯수(필수) | |
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
//Item(Cell) 꾸미기(필수) | |
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
//Item(Cell) 클릭시(선택) | |
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath | |
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
//Item(Cell) 클릭해제시(선택) | |
- (void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath | |
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath | |
//Object Library 아이템 | |
//1. Collection View - (Base 가 되는 View, TableView와 동일 하다고 생각하면 된다.) | |
//2. Collection View Cell - (Collection View 에 하나하나의 Item, TableViewCell과 동일 하다고 생각하면 된다.) | |
//3. Collection Reusable View - (Header 나 Footer 를 설정할때 사용되는 View) | |
//1. making basic UICollectionView | |
//UICollectionViewDemo1ViewController.h | |
//프로토콜 상속 : <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> | |
@interface UICollectionViewDemo1ViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> | |
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView; | |
@end | |
//UICollectionViewDemo1ViewController.m | |
@implementation UICollectionViewDemo1ViewController | |
@synthesize collectionView; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.navigationItem.title = @"UICollection Sample..."; | |
// DataSource... Delegate... | |
[collectionView setDataSource:self]; | |
[collectionView setDelegate:self]; | |
} | |
// Section for Item Count... | |
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ | |
return 15; | |
} | |
// CollectionViewCell Item Create... | |
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { | |
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; | |
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.contentView.frame]; | |
imgView.image = [UIImage imageNamed:@"sample.png"]; | |
[cell.contentView addSubview:imgView]; | |
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 100, 30)]; | |
[lable setText:[NSString stringWithFormat:@"[JW]%d-%d",indexPath.section,indexPath.row]]; | |
[cell.contentView addSubview:lable]; | |
return cell; | |
} | |
@end | |
//2. section 나누기 | |
//1) 프로그램 방 | |
//UICollectionViewDemo2ViewController.m | |
// CollectionView Sections Count... | |
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView { | |
return 3; | |
} | |
- (void)viewDidLoad | |
{ | |
//... | |
// Section Size... Layout Setting... | |
UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout; | |
collectionViewLayout.sectionInset = UIEdgeInsetsMake(20, 0, 20, 0); //top 20, left, bottom 20, right | |
} | |
//2) 스토리보드 이용 | |
//StoryBoard에서 Collection Reusable View를 상단에 배치 | |
//1. Attributes inspector > Reusable ID : @"Header" | |
//2. Size : H 30 적용 | |
// Section Header Create... | |
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ | |
if([kind isEqualToString:UICollectionElementKindSectionHeader]){ | |
UICollectionReusableView *rView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath]; | |
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)]; | |
[lable setText:[NSString stringWithFormat:@"%d번 라인",indexPath.section]]; | |
[lable setTextAlignment:NSTextAlignmentRight]; | |
[lable setFont:[UIFont systemFontOfSize:20.0f]]; | |
[lable setBackgroundColor:[UIColor yellowColor]]; | |
[rView addSubview:lable]; | |
return rView; | |
} | |
return Nil; | |
} | |
//3. Selecting, DeSelecting Cell | |
//UICollectionViewDemo2ViewController.m | |
// Select Item... | |
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { | |
NSLog(@"did SelectItem %d-%d",indexPath.section,indexPath.row); | |
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; | |
cell.layer.borderColor = [UIColor blueColor].CGColor; | |
cell.layer.borderWidth = 3.0f; | |
} | |
// De Select Item... | |
- (void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { | |
NSLog(@"did De selectItem %d-%d",indexPath.section,indexPath.row); | |
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; | |
cell.layer.borderColor = nil; | |
cell.layer.borderWidth = 0.0f; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment