//
// RootViewController.m
// Ui - 19 _ UICollectionView
//
// Created by dllo on 15/12/3.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "RootViewController.h"
#import "CustomCollectionViewCell.h"
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//uicollectviewLayout 是一个抽象类,我们一般使用他的子类UICollectionViewFlowLayout
UICollectionViewFlowLayout *flowl = [[[UICollectionViewFlowLayout alloc]init]autorelease];
//行间距
//注意:1, 系统会尽可能靠近此参数,并保证不会小于
//垂直与滑动方向即为行!
flowl.minimumLineSpacing = 50;
//列间距
flowl.minimumInteritemSpacing = 50;
//单元大小
flowl.itemSize = CGSizeMake(100, 100);
//区头间据
flowl.headerReferenceSize = CGSizeMake(50, 50);
//区脚间距
flowl.footerReferenceSize = CGSizeMake(100, 100);
//与屏幕四周的间距
flowl.sectionInset = UIEdgeInsetsMake(10, 30, 10, 30);
//滚动方向
flowl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowl];
collectionV.backgroundColor = [UIColor purpleColor];
collectionV.delegate = self;
collectionV.dataSource = self;
[self.view addSubview:collectionV];
[collectionV release];
//注册自定义cell
[collectionV registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
// Do any additional setup after loading the view.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.label1.text =[NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 3;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击%ld", indexPath.row);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// CustomCollectionViewCell.h
// Ui - 19 _ UICollectionView
//
// Created by dllo on 15/12/3.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CustomCollectionViewCell : UICollectionViewCell
@property (nonatomic, retain)UILabel *label1;
@end
//
// CustomCollectionViewCell.m
// Ui - 19 _ UICollectionView
//
// Created by dllo on 15/12/3.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "CustomCollectionViewCell.h"
@implementation CustomCollectionViewCell
- (void)dealloc
{
[_label1 release];
[super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self create];
}
return self;
}
- (void)create
{
self.backgroundColor = [UIColor redColor];
self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(0,0 , 30, 30)];
[self.contentView addSubview:self.label1];
self.label1.backgroundColor = [UIColor whiteColor];
[self.label1 release];
}
@end