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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序