#import "RootViewController.h"
#import "DetailViewController.h"
static NSString *string = @"cell";
static NSString *headerIndetifier = @"header";
static NSString *footerIndetifier = @"footer";
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.title = @"集合视图";
//UICollectionView,集合视图, UITableView的好基友,与他十分相似
//UICollectionView,继承于UIScrollView
//UICollectionViewLayout,集合视图的布局.继承于NSObject,控制和管理集合视图的布局,是抽象基类,不能直接使用
//UICollectionViewFlowLayout,UICollectionViewLayout的子类,可以使用
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//cell的大小
flowLayout.itemSize = CGSizeMake(50, 50);
//cell在X轴上的间距,默认10
flowLayout.minimumInteritemSpacing = 10;
//cell在Y轴上的间距,默认10
flowLayout.minimumLineSpacing = 10;
//滚动方向,默认垂直滚动
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//垂直滚动
//分区间距
flowLayout.sectionInset = UIEdgeInsetsMake(50, 20, 50, 20);//上左下右
//区头大小
flowLayout.headerReferenceSize = CGSizeMake(375, 50);
//区尾大小
flowLayout.footerReferenceSize = CGSizeMake(375, 50);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.dataSource = self;//设置数据源
collectionView.delegate = self;//设置代理
//注册cell类型
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:string];
//注册区头类型
[collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIndetifier];
//注册区尾类型
[collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIndetifier];
[self.view addSubview:collectionView];
[collectionView release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionViewDataSource
//某个分区有多少个cell(单元格)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
//展现cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//UICollectionViewCell,集合视图单元格,继承于UICollectionReusableView
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
//随机颜色
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000];
return cell;
}
//分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
//创建附加视图(区头和区尾)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//根据kind判断是区头或区尾
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//区头
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIndetifier forIndexPath:indexPath];
headerView.backgroundColor = [UIColor colorWithRed:0.272 green:1.000 blue:0.725 alpha:1.000];
return headerView;
}else {
//区尾
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIndetifier forIndexPath:indexPath];
footerView.backgroundColor = [UIColor colorWithRed:0.738 green:0.970 blue:1.000 alpha:1.000];
return footerView;
}
}
#pragma mark - UICollectionViewDelegate
//点击了某一个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"点击了:%ld--%ld", indexPath.section, indexPath.item);
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.color = collectionView.backgroundColor;
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
@end
#import "DetailViewController.h"
static NSString *string = @"cell";
static NSString *headerIndetifier = @"header";
static NSString *footerIndetifier = @"footer";
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.title = @"集合视图";
//UICollectionView,集合视图, UITableView的好基友,与他十分相似
//UICollectionView,继承于UIScrollView
//UICollectionViewLayout,集合视图的布局.继承于NSObject,控制和管理集合视图的布局,是抽象基类,不能直接使用
//UICollectionViewFlowLayout,UICollectionViewLayout的子类,可以使用
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//cell的大小
flowLayout.itemSize = CGSizeMake(50, 50);
//cell在X轴上的间距,默认10
flowLayout.minimumInteritemSpacing = 10;
//cell在Y轴上的间距,默认10
flowLayout.minimumLineSpacing = 10;
//滚动方向,默认垂直滚动
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//垂直滚动
//分区间距
flowLayout.sectionInset = UIEdgeInsetsMake(50, 20, 50, 20);//上左下右
//区头大小
flowLayout.headerReferenceSize = CGSizeMake(375, 50);
//区尾大小
flowLayout.footerReferenceSize = CGSizeMake(375, 50);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.dataSource = self;//设置数据源
collectionView.delegate = self;//设置代理
//注册cell类型
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:string];
//注册区头类型
[collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIndetifier];
//注册区尾类型
[collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIndetifier];
[self.view addSubview:collectionView];
[collectionView release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionViewDataSource
//某个分区有多少个cell(单元格)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
//展现cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//UICollectionViewCell,集合视图单元格,继承于UICollectionReusableView
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
//随机颜色
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.000];
return cell;
}
//分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
//创建附加视图(区头和区尾)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//根据kind判断是区头或区尾
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//区头
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIndetifier forIndexPath:indexPath];
headerView.backgroundColor = [UIColor colorWithRed:0.272 green:1.000 blue:0.725 alpha:1.000];
return headerView;
}else {
//区尾
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIndetifier forIndexPath:indexPath];
footerView.backgroundColor = [UIColor colorWithRed:0.738 green:0.970 blue:1.000 alpha:1.000];
return footerView;
}
}
#pragma mark - UICollectionViewDelegate
//点击了某一个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"点击了:%ld--%ld", indexPath.section, indexPath.item);
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.color = collectionView.backgroundColor;
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
@end