1. #import "ViewController.h"
  2. #import "CollectionViewCell.h"
  3. @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  4. @property (weak, nonatomic) IBOutlet UICollectionView *collectionCell;
  5. @end
  1. @implementation ViewController
  2. - (void)viewDidLoad
  3. {
  4. [super viewDidLoad];
  5. self.collectionCell.backgroundColor = [UIColor greenColor];
  6. self.collectionCell.dataSource = self;
  7. self.collectionCell.delegate = self;
  8. }
  9. #pragma mark -- UICollectionViewDataSource
  10. //设置collectionCell的个数
  11. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  12. {
  13. return 9;
  14. }
  15. //定义展示的Section的个数
  16. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  17. {
  18. return 1;
  19. }
  20. //每个UICollectionView展示的内容
  21. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  22. {
  23. //使用collectionCell进行注册,通过xib文件加载视图
  24. [self.collectionCell registerNib:[UINib nibWithNibName:@"myCell" bundle:nil]
  25. forCellWithReuseIdentifier:@"mycell"];
  26. CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
  27. //图片名称
  28. NSString *imageToLoad = [NSString stringWithFormat:@"%ld.jpg", indexPath.row];
  29. //加载图片
  30. cell.iamgeView.image = [UIImage imageNamed:imageToLoad];
  31. //设置label文字
  32. cell.labeiInfo.text = [NSString stringWithFormat:@"image%ld",indexPath.row];
  33. return cell;
  34. }
  35. //设置每个各自的大小
  36. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  37. {
  38. return CGSizeMake(170, 170);
  39. }
  40. //定义每个UICollectionView 的 margin
  41. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  42. {
  43. return UIEdgeInsetsMake(5, 5, 5, 5);
  44. }
  45. //UICollectionView被选中时调用的方法
  46. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  47. {
  48. UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  49. cell.backgroundColor = [UIColor whiteColor];
  50. }
  51. //返回这个UICollectionView是否可以被选择
  52. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  53. {
  54. return YES;
  55. }
  56. //隐藏状态栏
  57. -(BOOL)prefersStatusBarHidden
  58. {
  59. return YES;
  60. }
  61. @end

程序运行结果:

collectionView的案例的更多相关文章

  1. Dynamic支持CollectionView布局 、 MotionEffects特效 、 BlurImage效果 、 TextKit

    1 使用UIDynamicAnimator对集合视图进行布局 1.1 问题 UIKit Dynamic动力模型一个非常有趣的用途就是影响集合视图的布局,可以给集合视图的布局添加各种动力行为,使其产生丰 ...

  2. 数据库优化案例——————某市中心医院HIS系统

    记得在自己学习数据库知识的时候特别喜欢看案例,因为优化的手段是容易掌握的,但是整体的优化思想是很难学会的.这也是为什么自己特别喜欢看案例,今天也开始分享自己做的优化案例. 最近一直很忙,博客产出也少的 ...

  3. SQL Server内存遭遇操作系统进程压榨案例

    场景: 最近一台DB服务器偶尔出现CPU报警,我的邮件报警阈(请读yù)值设置的是15%,开始时没当回事,以为是有什么统计类的查询,后来越来越频繁. 探索: 我决定来查一下,究竟是什么在作怪,我排查的 ...

  4. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  5. Yeoman 官网教学案例:使用 Yeoman 构建 WebApp

    STEP 1:设置开发环境 与yeoman的所有交互都是通过命令行.Mac系统使用terminal.app,Linux系统使用shell,windows系统可以使用cmder/PowerShell/c ...

  6. 了不起的 nodejs-TwitterWeb 案例 bug 解决

    了不起的nodejs算是一本不错的入门书,不过书中个别案例存在bug,按照书中源码无法做出和书中相同效果,原本兴奋的心情掺杂着些许失落. 现在我们看一下第七章HTTP,一个Twitter Web客户端 ...

  7. 一个表缺失索引发的CPU资源瓶颈案例

    背景 近几日,公司的应用团队反应业务系统突然变慢了,之前是一直比较正常.后与业务部门沟通了解详情,得知最近生意比较好,同时也在做大的促销活动,使得业务数据处理的量出现较大的增长,最终系统在处理时出现瓶 ...

  8. 【Machine Learning】决策树案例:基于python的商品购买能力预测系统

    决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...

  9. Redis简单案例(二) 网站最近的访问用户

    我们有时会在网站中看到最后的访问用户.最近的活跃用户等等诸如此类的一些信息.本文就以最后的访问用户为例, 用Redis来实现这个小功能.在这之前,我们可以先简单了解一下在oracle.sqlserve ...

随机推荐

  1. GoogleMap-------manifest文件配置

    前言:在使用GoopleMap之前需要配置manifest文件 1.这个可有可无,com.xhm.meishi是项目的包名 <!-- 声明调用这个应用需要的权限 --> <permi ...

  2. C++11写算法之选择排序

    选择排序,顾名思义,指从数组后面将最小的值找出来,然后与最前面(指当前位置)值进行交换. 时间复杂度:O(n^2) 空间复杂度:O(1) 此处应用了C++11的auto , lambda , stat ...

  3. 【网络优化|渣速必看】合理设置MTU,提升网络速度

    可能很少网友注意过“本机.网络”的“MTU”值对自己网络性能产生的影响.对于追求更快的下载速度来说,MTU值设置不当,就仿佛穿着高跟鞋跑步一般. MTU是什么? “MTU=最大传输单元 单位:字节” ...

  4. 5-1、easyUI-菜单与按钮(上节问题与解决)

    首先把上节的代码copy过来,如下: <html> <head> <meta http-equiv="Content-Type" content=&q ...

  5. fork小续

    pid_t pid = fork(); 1.根据fork的返回值区分父子进程: fork 函数返回两次, >0 表示父进程,返回值为子进程ID; =0 表示子进程; <0 出错. 可用代码 ...

  6. n++ ++n

    n = 100 + m++ n = 100 +m; ++m n = 100 + ++m; ++m; n = 100 +m ;

  7. phpwind 论坛 转移

    前段时间用phpwind 搭建了一个本地论坛系统,也写过一篇随笔,讲phpwind论坛的迁移,昨天网上又对论坛做了迁移,在本地搭建了系统. 使用的是之前没有成功的方法.这种方法挺方便的,之前每次都是重 ...

  8. Exponential Backoff

    f^x(f^y+f-m)+f-n =f^(x+y)+f^(f-m)+(f-n) <?php $exponent=0; w(80,3); function w($input,$base){ glo ...

  9. es6数组的一些函数方法使用

    数组函数forEach().map().filter().find().every().some().reduce()等 数组函数(这里的回调函数中的index和arr都可以省略,回调函数后有参数是设 ...

  10. springcloud zuul

    zuul是springcloud的API网关. 入口也是springmvc的DispatcherServlet. 实际的handler是ZuulController,通过handleRequest方法 ...