创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset。设置对应的布局大小,相关的和顶部之间的间距等。

UICollectionView创建对应的view并且定义对应的大小,设置代理方法和数据源对象。

-(void)loadCollectionView
{
UICollectionViewFlowLayout * collectionViewFlow = [[UICollectionViewFlowLayout alloc]init];
UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(, ScreenHeight, CGRectGetWidth(_homeScrollView.frame), )collectionViewLayout:collectionViewFlow]; collectView.dataSource = self;
collectView.delegate = self;
collectView.backgroundColor = [UIColor blueColor];
[collectView registerClass:[CSHomeCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
collectionViewFlow.itemSize = CGSizeMake(, );
collectionViewFlow.sectionInset = UIEdgeInsetsMake(, , , );
collectionViewFlow.scrollDirection = UICollectionViewScrollDirectionVertical;
[_homeScrollView addSubview:collectView];
}
//datasource,delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CSHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
cell.topImage.image = [UIImage imageNamed:@"cludy_bg"];
return cell;
}

从而来实现对应的方法,其中自定义的cell类

-(instancetype)initWithFrame:(CGRect)frame
{
self =[super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
self.topImage = [[UIImageView alloc]initWithFrame:CGRectMake(, , MYSIZE.width-, MYSIZE.height*0.6)];
[self addSubview:_topImage]; self.designer = [[UILabel alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.topImage.frame), CGRectGetWidth(self.frame), )]; self.designer.text = @"hello";
[self addSubview:_designer]; // self.goodBtn = [UIButton alloc]initWithFrame:CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>) }
return self;
}

当定义的时候,自动调用initwithframe函数来初始化

collectionview使用的更多相关文章

  1. 用collectionview实现瀑布流-转(后面附demo,供参考)

    算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...

  2. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  3. tableViewCell嵌套collectionView,动态高度

    方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...

  4. iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调

    UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...

  5. WPF CollectionViewSource CollectionView

    CollectionView 通俗讲就是可以对你绑定的集合可以进行 分组,排序 等功能 CollectionViewSource  根据字面意思是xxx的数据源 详细的介绍还是看 http://www ...

  6. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  7. collectionview cell吸顶效果

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...

  8. collectionView

    // /* UICollectionView 类是iOS6 新引进的API,用于展示集合视图, 布局更加灵活,可实现多列布局,用法类似于UITableView类. - 更新视图: [collectio ...

  9. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

随机推荐

  1. 获取Ca证书相关与服务器信息

    Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提 ...

  2. 如何用js定义数组,用js来拼接json字段

    定义js数组的方式有: var arr = (); var arr = []; var arr = new Array(); 如何拼接成一个json字段. <!DOCTYPE HTML PUBL ...

  3. JDBC连接数据库(PreparedStatement)

    PreparedStatement是在数据库端防止SQL注入漏洞的SQL方法这里演示了一些基本使用方法同样使用Oracle数据库,之前已经手动建立了一张t_account表数据库代码参见上一篇< ...

  4. 苹果Home键恢复(无工具篇)

    无工具法: 弹指神功.用手指轻轻弹Home,过了一阵子后可能会出现白色污垢,其后再重覆“弹”,直至正常为止.(亲测可用) 软件调试法.首先,打开任意一款应用程序,按住电源开关几秒钟,直到屏幕出现滑动关 ...

  5. git 版本控制

    用gitbash进入类似命令行的窗口 用命令 cd e:/learngit 进入该目录,然后在此目录下初始化$ git init, 于是该文件夹就成为了一个工作区,里面的.git文件就是版本库(rep ...

  6. Python循环语句

    1.Python循环类型 1.while循环:在某条件下,循环执行某段程序 a. while语句有两个重要命令:continue,break来跳出循环. continue用来跳出该次循环 break用 ...

  7. HTML 兼容性

    1. 不同浏览器对HTML标记所具有的内外边距属性具有不同的定义. 2. 因此如果想消除这种差距,应该在相应的CSS部分加入以下CSS代码: *{margin:0px;padding:0px;} 优先 ...

  8. UVa 221城市正视图(离散化)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. CE STEPLDR

    作用:初始化CPU.内存.Flash,复制EBoot到内存并跳入EBoot中运行. 原理:S3C2416有 8-KB 的steppingstone(暂时翻译为垫脚石),在Nand启动模式下可把Nand ...

  10. POJ 3666 Making the Grade

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...