UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

下面给出一些常用方法,具体的使用可以参考Demo:点我下载  苹果官方Demo:点我下载

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.title = @"UICollectionView学习";
  5. //通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell
  6. [self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];
  7. //注册headerView Nib的view需要继承UICollectionReusableView
  8. [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
  9. //注册footerView Nib的view需要继承UICollectionReusableView
  10. [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];
  11. //
  12. self.collectionView.allowsMultipleSelection = YES;//默认为NO,是否可以多选
  13. }
  14. - (void)didReceiveMemoryWarning
  15. {
  16. [super didReceiveMemoryWarning];
  17. // Dispose of any resources that can be recreated.
  18. }
  19. #pragma mark -CollectionView datasource
  20. //section
  21. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  22. {
  23. return 2;
  24. }
  25. //item个数
  26. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  27. {
  28. return 6;
  29. }
  30. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  31. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  32. {
  33. //重用cell
  34. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];
  35. //赋值
  36. UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
  37. UILabel *label = (UILabel *)[cell viewWithTag:2];
  38. NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];
  39. imageView.image = [UIImage imageNamed:imageName];
  40. label.text = imageName;
  41. cell.backgroundColor = [UIColor redColor];
  42. return cell;
  43. }
  44. // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
  45. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  46. NSString *reuseIdentifier;
  47. if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
  48. reuseIdentifier = kfooterIdentifier;
  49. }else{
  50. reuseIdentifier = kheaderIdentifier;
  51. }
  52. UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
  53. UILabel *label = (UILabel *)[view viewWithTag:1];
  54. if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
  55. label.text = [NSString stringWithFormat:@"这是header:%d",indexPath.section];
  56. }
  57. else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
  58. view.backgroundColor = [UIColor lightGrayColor];
  59. label.text = [NSString stringWithFormat:@"这是footer:%d",indexPath.section];
  60. }
  61. return view;
  62. }
  63. //定义每个UICollectionViewCell 的大小
  64. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. return CGSizeMake(60, 80);
  67. }
  68. //定义每个Section 的 margin
  69. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  70. {
  71. return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右
  72. }
  73. //返回头headerView的大小
  74. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  75. CGSize size={320,45};
  76. return size;
  77. }
  78. //返回头footerView的大小
  79. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  80. {
  81. CGSize size={320,45};
  82. return size;
  83. }
  84. //每个section中不同的行之间的行间距
  85. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  86. {
  87. return 10;
  88. }
  89. //每个item之间的间距
  90. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  91. //{
  92. //    return 100;
  93. //}
  94. //选择了某个cell
  95. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  98. [cell setBackgroundColor:[UIColor greenColor]];
  99. }
  100. //取消选择了某个cell
  101. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  104. [cell setBackgroundColor:[UIColor redColor]];
  105. }

iOS UICollectionView简单使用的更多相关文章

  1. ios UICollectionView简单说明

    原谅我记不住写下来好了 UICollectionViewFlowLayout 流式自动布局 继承于UICollectionViewLayout 初始化:[[UICollectionViewFlowLa ...

  2. iOS上简单推送通知(Push Notification)的实现

    iOS上简单推送通知(Push Notification)的实现 根据这篇很好的教程(http://www.raywenderlich.com/3443/apple-push-notification ...

  3. iOS CAReplicatorLayer 简单动画

    代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...

  4. iOS之简单瀑布流的实现

    iOS之简单瀑布流的实现   前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里. 实现思路 collectionView能实现各中吊炸天的布局,其精髓就在于UICollectionVie ...

  5. IOS基金会_ UICollectionView简单易用

    和表格视图类似 UICollectionView的使用有两种方法 一种是继承UICollectionViewController,这个Controller会自带一个UICollectionView. ...

  6. iOS UICollectionView(转三)

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  7. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  8. [iOS] UICollectionView初始化滚动到中间的bug

    转载请保留地址wossoneri.com 问题 首先看一下我之前写的demo:link demo是封装了一个控件,直接在MainViewController的viewWillAppear里初始化,并且 ...

  9. iOS,手势识别简单使用

    1.iOS目前支持的手势识别(6种) 2.点按手势和慢速拖动手势简单使用 iOS目前支持的手势识别(6种) UITapGestureRecognizer(点按) UIPinchGestureRecog ...

随机推荐

  1. Js apply() call()使用详解

    Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...

  2. 利用dropbox备份vps数据

    在VPS的数据最好定时备份,免得服务器出了什么问题,数据就全丢了.我使用dropbox定时同步wordpress文件夹和数据库信息. 首先下载dropbox ? 1 wget -O dropbox.t ...

  3. ASP.NET中利用Split实现对Checkbox的字符串分离放到DataTable里面

    一.背景 昨天唐欢问了我一个问题: 现在有一个CheckBox和一个Label如下图: 要实现选中CheckBox,点击下面打印按钮的时候要做成这个样子的如下图: 简单的说就是档案编号作为表中的一个列 ...

  4. boost-内存管理

    Boost智能指针——scoped_ptr boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放. boost::sco ...

  5. spring中Bean的注入参数详解

    字面值    一般指可用字符串表示的值,这些值可以通过<value>元素标签进行注入.在默认情况下,基本数据类型及其封装类.String等类型都可以采取字面值注入的方式,Spring容器在 ...

  6. KAFKA分布式消息系统

    2015-01-05 大数据平台 Hadoop大数据平台 基本概念 kafka的工作方式和其他MQ基本相同,只是在一些名词命名上有些不同.为了更好的讨论,这里对这些名词做简单解释.通过这些解释应该可以 ...

  7. C++中的lambda表达式

    1.基本形式: [捕获列表](参数列表){函数体};     其中捕获列表和函数体不能省略但是捕获列表可以为空,也就是说最简单的lambda表达式是:  []{}; 2.lambda表达式又叫匿名函数 ...

  8. Careercup - Facebook面试题 - 5998719358992384

    2014-05-02 00:22 题目链接 原题: Given a matrix consisting of 's. 题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个. 解法:四邻接还是八邻 ...

  9. 【转】如何设置Android软键盘的默认不弹出?

    在开发Anroid的时候,当你打开一个界面的时候,屏幕的焦点会自动停留在第一个EditText中,Android的软键盘默认会自动弹出,用户第一眼连界面都没有看清楚,软键盘就弹出来了,这就影响到了用户 ...

  10. SQL Server备份事务日志结尾(Tail)

    原文:http://blog.csdn.net/tjvictor/article/details/5256906   事务日志结尾经常提交数据库未备份的事务日志内容.基本上,每一次你执行事务日志备份时 ...