//
// ViewController.m
// UICollectionView使用方法
//
// Created by yeyuanxiang on 16/5/19.
// Copyright © 2016年 KuYu_YX. All rights reserved.
//
#import "ViewController.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
* UICollectionView简介
*
* @return
UITableView的胞弟,都是继承自UIScrollView,都是由Datasource和Delegate驱动的,但是它在UITableView的基础上又做了扩展,以用来构建更复杂的布局。
*/
[self loadUI];
}
-(void)loadUI
{
/*
//同一组当中,垂直方向:行与行之间的间距;水平方向:列与列之间的间距
@property (nonatomic) CGFloat minimumLineSpacing;
//垂直方向:同一行中的cell之间的间距;水平方向:同一列中,cell与cell之间的间距
@property (nonatomic) CGFloat minimumInteritemSpacing;
//每个cell统一尺寸
@property (nonatomic) CGSize itemSize;
//滑动反向,默认滑动方向是垂直方向滑动
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;
//每一组头视图的尺寸。如果是垂直方向滑动,则只有高起作用;如果是水平方向滑动,则只有宽起作用。
@property (nonatomic) CGSize headerReferenceSize;
//每一组尾部视图的尺寸。如果是垂直方向滑动,则只有高起作用;如果是水平方向滑动,则只有宽起作用。
@property (nonatomic) CGSize footerReferenceSize;
//每一组的内容缩进
@property (nonatomic) UIEdgeInsets sectionInset;
*/
//datasource和deledate为提供我们提供了cell,UICollectionViewFlowLayout为这个cell布局,想要做成自己想要的样子,就重写这个布局类!
//初始化
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//设置滚动方向
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
//为什么要早早的注册啊,难道不能没有这个对象的时候再创建吗?不行~UICollectionView和UITableView这点不一样,UICollectionView需要先注册到重用池,等需要的时候再去池里面取,如果像UITableView那样如果cell不存在的时候再去创建,就崩了~
//注册
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//代理设置
self.collectionView.delegate = self ;
self.collectionView.dataSource = self ;
[self.view addSubview:self.collectionView];
}
#pragma mark --collectionView的代理方法
//item的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1000;
}
//每个UICollectionView所显示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = @"cell";
//重用池取
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row) / 255.0) blue:((30 * indexPath.row) / 255.0) alpha:1.0f];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
label.textColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%ld",indexPath.row];
for (id subView in cell.contentView.subviews) {
[subView removeFromSuperview];
}
[cell.contentView addSubview:label];
return cell ;
}
/**
* @author KuYu_YX
//每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//设置每组的cell的边界
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//头部相关
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//尾部相关
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
*
* @since <#1.0#>
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UICollectionView使用方法
最新推荐文章于 2022-12-18 21:26:36 发布