//
// RootViewController.m
// LessonUICollectionView
//
#import "RootViewController.h"
@interface RootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
//UICollectionViewDelegateFlowLayout该类遵守了UICollectionViewDelegate
// 实际上UICollectionViewDelegateFlowLayout这个协议是UICollectionViewDelegate的子协议
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
// 创建一个集合视图 collectionView
- (void)addSubViews
{
// 初始化UICollectionView需要一个独立的类UICollectionViewFlowLayout来设置其布局
//UICollectionViewLayout是一个抽象类 本身没有具体功能 其功能是由其子类UICollectionViewFlowLayout来实现的
// Item 布局(网格状 布局)
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 行边距(相对于 上下滑动) 如果左右滑动 就是列边距
layout.minimumLineSpacing = 30;
// 列边距(相对于 上下滑动) 如果左右滑动 就是行边距
layout.minimumInteritemSpacing = 30;
// 设置Item的宽高
layout.itemSize = CGSizeMake(150, 200);
// 设置滑动方向(默认是上下滑动的)
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置表头 宽度不起作用
layout.headerReferenceSize = CGSizeMake(0, 100);
// 设置表尾 宽度不起作用
layout.footerReferenceSize = CGSizeMake(0, 100);
// 设置内边距
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
// 用上面的layout布局初始化集合视图
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
//设置代理
collectionView.dataSource = self;
collectionView.delegate = self;
// collectionView默认是黑的
collectionView.backgroundColor = [UIColor whiteColor];
// 显示视图
[self.view addSubview:collectionView];
[collectionView release];
[layout release];
// 注册你要用的cell
//Identifier 重用标示符一定要和下面的一致
//registerClass:<#(Class)#> cell的类 系统的或你自定义的cell所属类
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
// 注册表头
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView"];
// 注册表尾
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView"];
}
#pragma mark ------代理方法------
// 必须实现的两个方法 跟tableView一样
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
// 返回每个Item的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 返回每个Item的方法
// 这个方法里面包括了之前tableViewCell写的那一大堆 不需要再alloc创建,如果集合中有会取出复用如果没有他会自动创建不需要自己再创建
// 必须要注册cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// 系统没有像tableView一样 给咱们 提供布局方式
// 要使用UICollectionViewCell一般都是自定义再使用 跟tableView一样所有自定义控件都要加到contentView上
cell.contentView.backgroundColor = [UIColor redColor];
return cell;
}
// 返回分区数跟tableView一样 默认一个
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
// 设置表头 表尾 同一个代理方法来是实现
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// 有参数kind判断返回表头还是表尾
// 因为参数是字符串的 判断相同不能等号要用方法isEqualToString:
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 返回表头 需要去复用集合中得到
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath];
// 加颜色
headerView.backgroundColor = [UIColor greenColor];
return headerView;
} else {
// 返回表尾
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor yellowColor];
return footerView;
}
}
@end
iOS经典讲解之UICollectionView
最新推荐文章于 2024-12-01 15:51:07 发布