注意拖控件的時候 必定注意代理的實現緩存
//atom
// ViewController.m代理
// ImageViewcode
//orm
// Created by Lenny on 4/18/15.string
// Copyright (c) 2015 Lenny Kwok. All rights reserved.it
//io
#import "ViewController.h"class
#import "LKImageCell.h"import
#define Identifier @"collectionCell"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//進行cell的註冊
// 註冊cell(若是緩存池中沒有HMCellIdentifier對應的cell,就會自動建立HMCellIdentifier對應的註冊過的cell)
[self.collectionView registerClass:[LKImageCell class] forCellWithReuseIdentifier:Identifier];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 16;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LKImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];
cell.icon = [NSString stringWithFormat:@"minion_%02ld",indexPath.item + 1];
return cell;
}
@end
//
// LKImageCell.m
// ImageView
//
// Created by Lenny on 4/18/15.
// Copyright (c) 2015 Lenny Kwok. All rights reserved.
//
#import "LKImageCell.h"
@interface LKImageCell ()
@property(nonatomic,weak) UIImageView * imageView;
@end
@implementation LKImageCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImageView * imageView = [[UIImageView alloc]init];
[self addSubview:imageView];
self.imageView = imageView;
}
return self;
}
-(void)setIcon:(NSString *)icon
{
_icon = [icon copy];
self.imageView.image = [UIImage imageNamed:icon];
}
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat x = 5;
CGFloat y = 0;
CGFloat w = self.bounds.size.width - 2 * x;
CGFloat h = self.bounds.size.height;
self.imageView.frame = CGRectMake(x, y, w, h);
}
@end
//
// LKImageCell.h
// ImageView
//
// Created by Lenny on 4/18/15.
// Copyright (c) 2015 Lenny Kwok. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LKImageCell : UICollectionViewCell
@property(nonatomic , copy) NSString * icon;
@end