Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created February 26, 2014 04:22
Show Gist options
  • Save keicoder/9223523 to your computer and use it in GitHub Desktop.
Save keicoder/9223523 to your computer and use it in GitHub Desktop.
objective-c : using custom CollectionViewCell
//using custom CollectionViewCell
//TWPhotoCollectionViewCell.h
@interface TWPhotoCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *imageView;
@end
//TWPhotoCollectionViewCell.m
#import "TWPhotoCollectionViewCell.h"
#define IMAGEVIEW_BORDER_LENGTH 5
@implementation TWPhotoCollectionViewCell
//in case making TWPhotoCollectionViewCell in code
- (id)initWithFrame:(CGRect)frame
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
//cause making TWPhotoCollectionViewCell in storyboard
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (void)setup
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)];
[self.contentView addSubview:self.imageView];
}
@end
//TWPhotosCollectionViewController.m
#import "TWPhotosCollectionViewController.h"
#import "TWPhotoCollectionViewCell.h" //커스텀 컬렉션 뷰 셀
@implementation TWPhotosCollectionViewController
#pragma mark - UICollectionView 데이터 소스
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
static NSString *CellIdentifier = @"Photo Cell";
//커스텀 컬렉션 뷰 셀 사용
TWPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.imageView.image = [UIImage imageNamed:@"speaker.jpg"];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
return 5;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment