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

相关文章

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

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

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

mysql中的group by高级用法

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

Java中Scanner的用法示例小结

《Java中Scanner的用法示例小结》有时候我们在编写代码的时候可能会使用输入和输出,那Java也有自己的输入和输出,今天我们来探究一下,对JavaScanner用法相关知识感兴趣的朋友一起看看吧... 目录前言一 输出二 输入Scanner的使用多组输入三 综合练习:猜数字游戏猜数字前言有时候我们在

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程