//
/*
UICollectionView 类是iOS6 新引进的API,用于展示集合视图,
布局更加灵活,可实现多列布局,用法类似于UITableView类。
- 更新视图: [collectionView reloadData];
- 自定义UICollectionViewCell,与自定义tableViewCell基本一致
*/ #import "ViewController.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];//创建collectionView,要指定layout
collectionView.backgroundColor = [UIColor grayColor];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell的重用标识,也就是下面我们要使用的cell
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册头视图(每组)
// [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"remnant"];//注册尾视图
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
}
//几个代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;//设置组数
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;//设置每组单元数,过多会自动换行
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//设置cell,注意,此处使用的identifier必须与之前注册的保持一致
static NSString *identifier = @"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
// cell.selectedBackgroundView = nil;设置选中视图
cell.layer.borderWidth = 0.5;
cell.layer.borderColor = [UIColor redColor].CGColor;
return cell;
}
//如果想要使用头视图,则必须实现该方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//设置每组头视图的尺寸
return CGSizeMake(, );
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%s",__FUNCTION__);
//设置头/根视图,注意,此处的withReuseIdentifier要与之前注册时使用的完全一致(注意重用机制导致的bug)
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor orangeColor];
[headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UILabel *lable = [[UILabel alloc]initWithFrame:headerView.bounds];
lable.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
lable.textColor = [UIColor blueColor];
[headerView addSubview:lable];
return headerView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//否单元被点击
NSLog(@"%zi组,%zi列",indexPath.section,indexPath.item);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//设置item(视图元素)的尺寸
return CGSizeMake(, );
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//设置组的边距(上、左、下、右)
return UIEdgeInsetsMake(, , , );
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//两个item的列间距
return ;
} -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//如果一组中有多行item,设置行间距
return ;
}
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
// //设置每组尾视图的尺寸
// return CGSizeMake(100, 20);
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最新文章

  1. Caffe+CUDA7.5+CuDNNv3+OpenCV3.0+Ubuntu14.04 配置参考文献 以及 常见编译问题总结
  2. QCustomplot使用分享(一) 能做什么事
  3. Android 6.0 SDK 找不到HttpClient的解决方法
  4. android学习笔记23——菜单
  5. easyui tabs内容panel自适应窗体宽度方法
  6. ASP.NET MVC- VIEW Overview Part 1
  7. cocos2dx之触摸事件
  8. mysql sql学习(一)mysql连接
  9. Android 墙纸设置代码 详细说明
  10. SiganlR 系列之概述
  11. python 运用numpy库与matplotlib库绘制数据图
  12. PJ初赛复习日记
  13. Docker入门03——Container
  14. 《principal component analysis based cataract grading and classification》学习笔记
  15. MVC ---- IEnumerable&lt;T&gt;、List&lt;T&gt; 前台遍历展示
  16. 浏览器是怎样工作的:渲染引擎,HTML解析
  17. 4. python 修改字符串实例总结
  18. [Algorithm] Largest sum of non-adjacent numbers
  19. 泰德激光打标软件 包含 #include &quot;Main.h&quot; 时 原本正确的单元却报错
  20. ZT Android4.2关于bluetooth在HAL层的分析(1)

热门文章

  1. [转]java 输出流转输入流
  2. windows 内部预览版与迅雷极速版不配合
  3. 1031MVCC和事务浅析
  4. MySQL中的insert ignore into, replace into等的一些用法总结
  5. 初步认识ajax(个人整理)
  6. [转]java.sql.SQLException: 无效的列索引
  7. Hessian Matrix
  8. 2010-2014总结 ____V_V____ hello-world
  9. eclipse中如何远程java debug配置
  10. Sigmaplot激活码获取地址