无限滚动 UICollectionView
注意拖控件的时候 一定注意代理的实现
//
// ViewController.m
// ImageView
//
// Created by Lenny on 4/18/15.
// Copyright (c) 2015 Lenny Kwok. All rights reserved.
//
#import "ViewController.h"
#import "LKImageCell.h"
#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