UICollectionView 集合视图
2015-09-01 20:37
337 查看
#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
相关文章推荐
- Android UI设计小知识——富文本
- setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key解决办法
- UIView与CALayer的区别
- UITableView<二>
- UIView动画和CAAnimation动画
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
- Android UI设计小知识——渐变色背景的制作
- HDU 5014 Number Sequence
- UITableView <一>
- UIView动画 防多星球环绕转动
- Android UI设计小知识——按钮背景的制作
- FragmentTabHost切换Fragment时避免UI重新加载
- POJ 3368 Frequent values RMQ ST算法/线段树
- uestc893
- UICollectionView基础
- empty返回值为true的三种情况
- UVA 1612 Guess
- 559B - Equivalent Strings & 560D - Equivalent Strings
- UVa 11572 - Unique Snowflakes (滑动窗口STL_map||set) 好题!!!
- UITableView,UICollectionView,UIScrollView快速返回顶部