1. //
  2. // MallTestViewController.h
  3. // fitmiss
  4. //
  5. // Created by bill on 16/6/28.
  6. // Copyright © 2016年 lear. All rights reserved.
  7. //
  8.  
  9. #import "RootViewController.h"
  10.  
  11. @interface MallTestViewController : RootViewController
  12.  
  13. @end
  1. //
  2. // MallTestViewController.m
  3. // fitmiss
  4. //
  5. // Created by bill on 16/6/28.
  6. // Copyright © 2016年 lear. All rights reserved.
  7. //
  8.  
  9. #import "MallTestViewController.h"
  10.  
  11. // 注意const的位置
  12. static NSString *const cellId = @"cellId";
  13. static NSString *const headerId = @"headerId";
  14. static NSString *const footerId = @"footerId";
  15.  
  16. @interface MallTestViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  17.  
  18. @end
  19.  
  20. @implementation MallTestViewController
  21.  
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24.  
  25. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  26.  
  27. UICollectionView *colView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , [Function getScreenWidth], [Function getScreenHeight:self]) collectionViewLayout:layout];
  28. colView.backgroundColor = [UIColor grayColor];
  29. colView.dataSource = self;
  30. colView.delegate = self;
  31. [self.view addSubview:colView];
  32.  
  33. // 注册cell、sectionHeader、sectionFooter
  34. [colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
  35. [colView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
  36. [colView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
  37.  
  38. }
  39.  
  40. - (NSArray *)loadData
  41. {
  42. NSArray *arr = [NSArray arrayWithObjects:@"cell", @"cell2", @"cell3", @"cell4", @"cell5", @"cell6", @"cell7", @"cell8", @"cell9",nil];
  43. return arr;
  44. }
  45.  
  46. #pragma mark - UICollectionViewDataSource
  47. // 定义展示的Section的个数
  48. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  49. {
  50. return ;
  51. }
  52.  
  53. // 定义每个 UICollectionView 中展示的 UICollectionViewCell 的个数
  54. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  55. {
  56. return [[self loadData] count];
  57. }
  58.  
  59. // 每个 UICollectionView 展示的内容
  60. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  61. {
  62. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  63. cell.backgroundColor = [UIColor redColor];
  64. return cell;
  65. }
  66.  
  67. // 设置 UICollectionView 的段头段尾内容,和UITableView类似
  68. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  69. {
  70.  
  71. if([kind isEqualToString:UICollectionElementKindSectionHeader])
  72. {
  73. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
  74. if(headerView == nil)
  75. {
  76. headerView = [[UICollectionReusableView alloc] init];
  77. }
  78. headerView.backgroundColor = [UIColor whiteColor];
  79.  
  80. return headerView;
  81. }
  82. else if([kind isEqualToString:UICollectionElementKindSectionFooter])
  83. {
  84. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
  85. if(footerView == nil)
  86. {
  87. footerView = [[UICollectionReusableView alloc] init];
  88. }
  89. footerView.backgroundColor = [UIColor lightGrayColor];
  90.  
  91. return footerView;
  92. }
  93.  
  94. return nil;
  95. }
  96.  
  97. // 是否可移动
  98. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
  99. {
  100. return YES;
  101. }
  102.  
  103. // 处理移动
  104. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
  105. {
  106.  
  107. }
  108.  
  109. #pragma mark - UICollectionViewDelegateFlowLayout
  110.  
  111. // 定义 UICollectionView 中 cell 的尺寸
  112. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  113. {
  114. if (indexPath.section == ) {
  115. return CGSizeMake(, );
  116. }
  117. return CGSizeMake(, );
  118. }
  119.  
  120. // 定义 UICollectionView 的 margin
  121. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  122. {
  123. return UIEdgeInsetsMake(, , , );
  124. }
  125.  
  126. // 定义 UICollectionView 中上下两个 cell 的最小间距
  127. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  128. {
  129. return .f;
  130. }
  131.  
  132. // 定义 UICollectionView 中左右两个 cell 的最小间距
  133. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  134. {
  135. return .f;
  136. }
  137.  
  138. // 定义 UICollectionView 头部的尺寸
  139. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  140. if (section==) {
  141. return CGSizeMake(, );
  142. }
  143. return CGSizeMake(, );
  144. }
  145.  
  146. // 定义 UICollectionView 尾部的尺寸
  147. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  148. {
  149. return CGSizeMake(, );
  150. }
  151.  
  152. #pragma mark - UICollectionViewDelegate
  153.  
  154. // UICollectionView被选中时调用的方法
  155. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157.  
  158. NSLog(@"您点击了item:%@", [[self loadData] objectAtIndex:indexPath.row]);
  159. UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  160. //cell.backgroundColor = [UIColor whiteColor];
  161.  
  162. }
  163.  
  164. // 返回这个UICollectionView是否可以被选择
  165. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. return YES;
  168. // if (indexPath.row % 2)
  169. // {
  170. // return YES;
  171. // }
  172. // return NO;
  173. }
  174.  
  175. // 开启突出某个内容
  176. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. return YES;
  179. }
  180.  
  181. // 按下时处理内容 如:变色
  182. - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  185. cell.backgroundColor = [UIColor blueColor];
  186. }
  187.  
  188. // 松开时处理内容 如:变色
  189. - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  192. cell.backgroundColor = [UIColor greenColor];
  193. }
  194.  
  195. // 长按某item,弹出copy和paste的菜单
  196. - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
  197. {
  198. return YES;
  199. }
  200.  
  201. // 使copy和paste有效
  202. - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
  203. {
  204. if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"])
  205. {
  206. return YES;
  207. }
  208.  
  209. return NO;
  210. }
  211.  
  212. //处理copy和paste
  213. - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
  214. {
  215.  
  216. // if([NSStringFromSelector(action) isEqualToString:@"copy:"])
  217. // {
  218. // // NSLog(@"-------------执行拷贝-------------");
  219. // [collectionView performBatchUpdates:^{
  220. // [_section0Array removeObjectAtIndex:indexPath.row];
  221. // [collectionView deleteItemsAtIndexPaths:@[indexPath]];
  222. // } completion:nil];
  223. // }
  224. // else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
  225. // {
  226. // NSLog(@"-------------执行粘贴-------------");
  227. // }
  228. }
  229.  
  230. @end

UICollectionView的使用小记录和一些说明的更多相关文章

  1. Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录)

    Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录) 作者:王可利(Star·星星) 效果图: 代码如下: class Star8 { public static void m ...

  2. Java 需要记得、了解的关键词 (Java 学习中的小记录)

    Java 需要记得.了解的关键词 (Java 学习中的小记录)     作者:王可利(Star·星星) 总结:本次随笔,仅且拿来平时翻阅记忆用

  3. 关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)

    关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)   作者:王可利(Star·星星) HTML中 相对定位:position:relative; 绝对定位:position ...

  4. 利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)

    利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)   作者:王可利(Star·星星) 总结: 今天学习的jQ类库的使用,代码重复的比较多需要完善.严格区分大小写,在 $(" ...

  5. html/css 盒子布局 Margin 、Padding 、border 以及 清除浮动的知识 (学习HTML过程中的小记录)

    html/css  盒子布局 Margin .Padding .border 以及 清除浮动的知识 (学习HTML过程中的小记录) 作者:王可利(Star·星星) width     是"宽 ...

  6. NDK开发小记录 C++读取java层对象内容

    这是自己NDK开发得小记录,关于C++层读取java层传来的对象内容. 很简单的一个例子, 1.首先在java层定义了一个类NumList: public class NumList { public ...

  7. centos 6.X minimal 系列最小化安装完成后,安装mono和jexus过程小记录

    在使用虚拟机安装minimal版centos运行mono+jexus的时候,遇到了一些坑,记录一下,比如虚拟机访问不了网络,没wget命令没开放80端口,等等小问题,其他网上教程已经有mono+jex ...

  8. Backbone小记录

    前言 这两天看了下Backbone.js的知识,大概了解了这个框架的一些知识. 写篇博客总结一下. Backbone.js是一个web端javascript的轻量级MVC框架.为什么说是轻量级呢?因为 ...

  9. HorizontalScrollView做页卡的一个小记录

    用HorizontalScrollView做页卡,实现一个如下图的效果:

随机推荐

  1. css 修改滚动条

    ::-webkit-scrollbar { width: 10px;}::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgb ...

  2. BZOJ 3176 Sort

    先一遍reverse+逆序对个数. 要开long long啊. #include<iostream> #include<cstdio> #include<cstring& ...

  3. schematool -dbType mysql -initSchema hive startup failed...try this

    schematool -dbType mysql -initSchema hive startup failed

  4. UE4 Android打包 问题 记录笔记

    问题一:error: expression result unused [-Werror,-Wunused-value] 虽然看了输出日志知道了这行沉余代码删掉就行,但是不是很懂这个地方报错意义. 问 ...

  5. MVC SSO登陆 的麻烦事~

    前段时间用MVC + Redis 做session搞了个简单的单点登录Web站.真是日了狗的问题多. 今天正好睡不着,做个备忘笔记>_< 实现方法很简单,无非就是从重载个Controlle ...

  6. Mybatis3.x与Spring4.x整合(转)

    http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...

  7. Jmeter增加压力机方法

    windows: 需要别人的机器也安装jmeter 在别人的机器上运行jmeter-server.bat 修改jmeter.properties文件,查找remote_hosts 原始:remote_ ...

  8. 移动销售端app的需求分析

    随着网络时代的发展,人们对于网络的依赖越来越大,网上购物便成了一个很大的消费者市场.. 如何分析一个综合的移动销售端app的需求我认为首先要确定用户,从用户的角度一个一个功能过,评估每一个功能的需求, ...

  9. 常用的CSS Hack

    一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...

  10. C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式

    前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...