UICollectionView

  

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

//collectionView布局初始化
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//定义每个Cell 的大小
flowLayout.itemSize = CGSizeMake(, );
//头部大小
flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, );
//定义每个Cell 纵向的间距
flowLayout.minimumLineSpacing = ;
//定义每个Cell 横向的间距
flowLayout.minimumInteritemSpacing = ;
//定义每个Cell到容器边缘 的边距
flowLayout.sectionInset = UIEdgeInsetsMake(, , , );//上左下右 //collectionView初始化
_collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
//背景颜色
_collectionView.backgroundColor = [UIColor greenColor];
//自适应大小
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//界面添加
[self.view addSubview:_collectionView];
//尺寸设置
[_collectionView setFrame:self.view.frame];
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
} //每个cell展示的内容的具体实现
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//注意单元格复用,注意identifier和之前注册时用的相同(@"cell")
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (!cell) {
cell = [[UICollectionViewCell alloc] init];
}
return cell;
} //cell被选中时触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"你点了一个单元格");
}

新建的UIcoolectionViewCell文件中放入这一句进行初始化:

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
//在这里写入对cell进行定制的内容
}
return self;
}

[OC] UIcollectionView 与 UIcollectionViewCell 的使用的更多相关文章

  1. UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug

    如题: UITableViewCell上放UICollectionView ,UICollectionViewCell无法复用bug 如果UITableViewCell的size大于整个collect ...

  2. UICollectionView基础/UICollectionViewCell的四种创建方式

    前言 UICollectionViewCell的四种创建方式:http://blog.csdn.net/ZC_Huang/article/details/52002302 这个控件,看起来与UITab ...

  3. 从此走上一条iOS程序猿不归路。。。

    新的城市,新的生活!前不久刚刚结束了苦逼的面试找工作之旅,期间也小有收货,如今正处年底工作闲暇之余,将前一阵子陆陆续续的总结整理了一下,本人菜鸟程序猿一只,水平有限,本文总结的知识不算深入,比较浅显, ...

  4. 自定义UICollectionViewLayout 布局实现瀑布流

    自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...

  5. UIKit框架使用总结--看看你掌握了多少

    一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...

  6. iOS 之UICollectionView 开发步骤 之 OC

    说起来容易做起来难. 那么我就不说了,来做吧.这就是我的style. 鉴于现在的主流还是OC,那么示例程序还用OC来写,后续补写Swift程序,这里先占个坑. 废话不多说,下面开发步骤来了: 1. 创 ...

  7. iOS开发——UI篇OC篇&UICollectionView详解+实例

    UICollectionView详解+实例 实现步骤: 一.新建两个类 1.继承自UIScrollView的子类,比如HMWaterflowView * 瀑布流显示控件,用来显示所有的瀑布流数据 2. ...

  8. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  9. 使用纯代码定义UICollectionView和自定义UICollectionViewCell

    1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...

随机推荐

  1. JGUI源码:Tip实现(14)

    tip是当鼠标放到控件上显示的提示文本,下面说下实现思路方法一: 使用hover:before,hover:after组合一个三角符号和一个圆角矩形实现,以右三角为例 .jgui-tip:after ...

  2. shell 生成任意大小文件

    $ dd if=/dev/zero of=junk.data bs=1M count=1 参数: if  (input file) of (output file) bs(block size) co ...

  3. SQL判断语句

    ,,decode(tts.execute_state,,'false','true')) from twf_tech_schedule tts sql判断语句

  4. jpa Auditor 自动赋值与自定义 @CreatedBy @LastModifiedBy @CreatedDate @LastModifiedDate

    在spring jpa audit 中,在字段或者方法上使用注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,当进行实体插入或者更 ...

  5. Map的几种取值方法

    public static void main(String[] args) throws IOException,ParseException { Map<String,String> ...

  6. python装饰器的wraps作用

    不加: from functools import wraps def my_decorator(func): def wper(*args, **kwargs): '''decorator''' p ...

  7. Java中的String、StringBuilder以及StringBuffer

    https://www.cnblogs.com/dolphin0520/p/3778589.html

  8. Linux代理搭建TinyProxy

    操作系统:阿里云CentOS 7.4 64位 安装方法: yum install tinyproxy 配置: vi /etc/tinyproxy/tinyproxy.conf Port 8888 // ...

  9. 那什么时候会触发BFC呢?块级格式化上下文

    <html>根元素: float的值不为none: overflow的值为auto.scroll或hidden: display的值为table-cell.table-caption和in ...

  10. Django-视图层(view)

    视图层(view) ​ 视图函数,简称视图,本质上是一个简单的Python函数,它接受Web请求并且返回Web响应.响应的内容可以是HTML网页,重定向,404错误,图片等任何东西,但本质是返回响应对 ...