iOS-UICollectionView用法

2024-04-08 08:48
文章标签 用法 ios uicollectionview

本文主要是介绍iOS-UICollectionView用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先认识一下UICollectionView

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. NS_CLASS_AVAILABLE_IOS(6_0@interface UICollectionView : UIScrollView  


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

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


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

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

效果图如下:

原文地址:http://blog.csdn.net/Apple_app/article/details/38867123

这篇关于iOS-UICollectionView用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/885117

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Mysql中isnull,ifnull,nullif的用法及语义详解

《Mysql中isnull,ifnull,nullif的用法及语义详解》MySQL中ISNULL判断表达式是否为NULL,IFNULL替换NULL值为指定值,NULLIF在表达式相等时返回NULL,用... 目录mysql中isnull,ifnull,nullif的用法1. ISNULL(expr) → 判

Java中的for循环高级用法

《Java中的for循环高级用法》本文系统解析Java中传统、增强型for循环、StreamAPI及并行流的实现原理与性能差异,并通过大量代码示例展示实际开发中的最佳实践,感兴趣的朋友一起看看吧... 目录前言一、基础篇:传统for循环1.1 标准语法结构1.2 典型应用场景二、进阶篇:增强型for循环2.