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

相关文章

Go 语言中的 Struct Tag 的用法详解

《Go语言中的StructTag的用法详解》在Go语言中,结构体字段标签(StructTag)是一种用于给字段添加元信息(metadata)的机制,常用于序列化(如JSON、XML)、ORM映... 目录一、结构体标签的基本语法二、json:"token"的具体含义三、常见的标签格式变体四、使用示例五、使用

mysql中的group by高级用法详解

《mysql中的groupby高级用法详解》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,本文给大家介绍mysql中的groupby... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

MySQL 字符串截取函数及用法详解

《MySQL字符串截取函数及用法详解》在MySQL中,字符串截取是常见的操作,主要用于从字符串中提取特定部分,MySQL提供了多种函数来实现这一功能,包括LEFT()、RIGHT()、SUBST... 目录mysql 字符串截取函数详解RIGHT(str, length):从右侧截取指定长度的字符SUBST

SQL Server中的PIVOT与UNPIVOT用法具体示例详解

《SQLServer中的PIVOT与UNPIVOT用法具体示例详解》这篇文章主要给大家介绍了关于SQLServer中的PIVOT与UNPIVOT用法的具体示例,SQLServer中PIVOT和U... 目录引言一、PIVOT:将行转换为列核心作用语法结构实战示例二、UNPIVOT:将列编程转换为行核心作用语

Java中 instanceof 的用法详细介绍

《Java中instanceof的用法详细介绍》在Java中,instanceof是一个二元运算符(类型比较操作符),用于检查一个对象是否是某个特定类、接口的实例,或者是否是其子类的实例,这篇文章... 目录引言基本语法基本作用1. 检查对象是否是指定类的实例2. 检查对象是否是子类的实例3. 检查对象是否

Java中的内部类和常用类用法解读

《Java中的内部类和常用类用法解读》:本文主要介绍Java中的内部类和常用类用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录内部类和常用类内部类成员内部类静态内部类局部内部类匿名内部类常用类Object类包装类String类StringBuffer和Stri

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Python 异步编程 asyncio简介及基本用法

《Python异步编程asyncio简介及基本用法》asyncio是Python的一个库,用于编写并发代码,使用协程、任务和Futures来处理I/O密集型和高延迟操作,本文给大家介绍Python... 目录1、asyncio是什么IO密集型任务特征2、怎么用1、基本用法2、关键字 async1、async

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils