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. dva解读1

    1.首先定义一个app对象实现dva const app = dva({ history: createHistory(), }); // 2. Plugins app.use(createLoadi ...

  2. 谈谈Android重打包--初语

    写在前面的话 仅以此系列献给喜欢我CSDN的小伙伴们 申明 此文禁止转载,谢谢合作 序言 在开头说这会是一个系列,那就说明我有非常多话要说.从最简单的介绍到问题的提出.解决方式的构思以及整个系统的架构 ...

  3. jprofiler_监控远程linux服务器的JVM进程(转 非常棒)

    几天前写了一篇文章,jprofiler_监控远程linux服务器的tomcat进程(实践),介绍了使用jprofiler怎样监控远程linux的tomcat进程,这两天想了想,除了可以监控tomcat ...

  4. UTI iPhone支持依文件后缀名打开应用

    本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6658492 iphoneapplicationprocessingfileurl ...

  5. ios如何在当前工程中添加编辑新建的FramesWork

    本文转载至 http://www.apkbus.com/android-131519-1-1.html,感谢原文作者的分享.     naniboy 该用户从未签到   可能很多大牛都见过FaceBo ...

  6. 01-K Code

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  7. 关于vue,angularjs1,react之间的对比

    1.时间投入的问题:相对于react和angularjs,学习vue的时间成本低,而且容易上手. 2.JSX的可读性比较一般.代码的可读性不如vue,当然,vue也支持jsx,但是vue更提倡temp ...

  8. mysql_表_操作

    1.创建表 # 基本语法: create table 表名( 列名 类型 是否可以为空 默认值 自增 主键, 列名 类型 是否可以为空 )ENGINE=InnoDB DEFAULT CHARSET=u ...

  9. 使用 mock 测试

    参考文章:https://semaphoreci.com/community/tutorials/getting-started-with-mocking-in-python What are the ...

  10. <2013 12 17> 专业技能

    Specialties: • Mechanical design modeling using Pro/ENGINEER and SolidWorks.• Robot control, path pl ...