下面是头文件(
FirstViewController
.h)代码
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *collectionView_;
}
.m文件代码 其中我自定义了一个Cell名为MultipleCell.h
#import "MultipleCell.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//UICollectionViewLayoutAttributes *flowLayout=[[UICollectionViewLayoutAttributes alloc]init];
//每个cell的大小
[flowLayout setItemSize:CGSizeMake(155,150)];
//设置方向(向下还是向右)垂直Vertical 水平Horizontal
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//设置距离边框的高度
flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);//top、lef、bottom、right
collectionView_ = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) collectionViewLayout:flowLayout];
[self.view addSubview:collectionView_];
collectionView_.backgroundColor = [UIColor redColor];
collectionView_.delegate = self;
collectionView_.dataSource = self;
[collectionView_ registerClass:[MultipleCell class] forCellWithReuseIdentifier:@"simpleCell"];
// Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"simpleCell";
MultipleCell *cell = (MultipleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
//cell = [[[MultipleCell alloc] initWithFrame:CGRectMake(0, 0, 40, 40)] autorelease];
}
cell.imgView.image = [UIImage imageNamed:@"01.png"];
cell.nameLabel.text = @"Weico";
cell.detailLabel.text = @"许士彦";
cell.markLabel.text =[NSString stringWithFormat:@"%d",indexPath.row];
//点击后背景图片
cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"01"]];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
自定义的cell.h文件
@property (nonatomic,retain)UIImageView *imgView;
@property (nonatomic,retain)UILabel *nameLabel;
@property (nonatomic,retain)UILabel *detailLabel;
@property (nonatomic,retain)UILabel *markLabel;
.m文件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:imgView];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 90, 30)];
nameLabel.backgroundColor = [UIColor clearColor];
[self addSubview:nameLabel];
detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, 60, 30)];
detailLabel.backgroundColor = [UIColor clearColor];
[self addSubview:detailLabel];
markLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 120, 30, 30)];
markLabel.backgroundColor = [UIColor clearColor];
markLabel.textColor = [UIColor greenColor];
[self addSubview:markLabel];
}
return self;
}