一、UICollectionView集合视图
 
        其继承自UIScrollView。
        UICollectionView类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView类。
1、需要遵循的协议:
1)UICollectionViewDataSource,
2)UICollectionViewDelegate,
3)UICollectionViewDelegateFlowLayout
2、创建collection:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];
   
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];//初始化,并设置布局方式
    collection.backgroundColor = [UIColor whiteColor];
   
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell,这是固定格式,也是必须要实现的
   
    [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];//注册头/尾视图,视图类型必须为UICollectionReusableView或者其子类,kind设置为UICollectionElementKindSectionHeader或者UICollectionElementKindSectionFooter,最后设置标识
   
    collection.delegate = self;
    collection.dataSource = self;
    [self.view addSubview:collection];
3、需要实现的方法:
1)-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    //设置组数,不写该方法默认是一组
    return 4;
}

2)-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}

3)-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
    static NSString *identifier = @"cell";//注意,此处的identifier要与注册cell时使用的标识保持一致
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
   
    cell.backgroundColor = [UIColor grayColor];
    cell.layer.borderWidth = 0.5;
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
    return cell;
   
}

//设置头视图的尺寸,如果想要使用头视图,则必须实现该方法
4)-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(300, 30);
}

5)-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //根据类型以及标识获取注册过的头视图,注意重用机制导致的bug
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor brownColor];
   
    for (UIView *view in headerView.subviews) {
        [view removeFromSuperview];
    }
   
    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
    label.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
    [headerView addSubview:label];
    label.textColor = [UIColor whiteColor];
   
    return headerView;
}

 
6)-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zi组,%zi行",indexPath.section,indexPath.item);
   
}

7)-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //设置item尺寸
    return CGSizeMake(self.view.frame.size.width/3.0, 100);
}
8)-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //设置组距离上向左右的间距
    return UIEdgeInsetsMake(0,0,0,0);
}

9)-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    //两个item的列间距
    return 0;
}

10)-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    //如果一组中有多行item,设置行间距
    return 0;

}
 
 
二、自定义集合视图
1、遵循的协议
1)UICollectionViewDataSource,
2)UICollectionViewDelegate,
3)UICollectionViewDelegateFlowLayout
 
2、创建
定义一个全局变量:
UICollectionView *_collectionView;
 
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
   
    [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
 
3、实现的方法
 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
   
    //自定义选中背景
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor grayColor];
   
    cell.selectedBackgroundView = view;
   
    return cell;

}
 
4、自定义一个继承自
CollectionViewCell的类为
MyCollectionViewCell
1)声明一个UIImageView属性
@property (nonatomic, strong) UIImageView *imageView;
2)实现方法
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_imageView];
       
    }
    return self;
}
 
 
 

UICollectionView(集合视图)以及自定义集合视图的更多相关文章

  1. UICollectionView 集合视图用法,自定义Cell

    在View里面 //1.创建UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFl ...

  2. springMVC源码解析--ViewResolverComposite视图解析器集合(二)

    上一篇博客springMVC源码分析--ViewResolver视图解析器(一)中我们介绍了一些springMVC提供的很多视图解析器ViewResolver,在开发的一套springMVC系统中是可 ...

  3. 集合、拆箱、装箱、自定义集合的foreach

    集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...

  4. 《C#本质论》读书笔记(16)构建自定义集合

    16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...

  5. 使用yield关键字让自定义集合实现foreach遍历

    一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...

  6. [c#基础]集合foreach的必要条件和自定义集合

    引言 最近翻看了之前的学习笔记,看到foreach,记得当时老师讲的时候,有点犯浑,不是很明白,这好比,上小学时,你不会乘法口诀,但是随着时间的增长,你不自觉的都会了,也悟出个小道理,有些东西,你当时 ...

  7. Qt之模型/视图(自定义进度条)

    简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...

  8. Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果

    开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...

  9. 十六、C# 常用集合类及构建自定义集合(使用迭代器)

    常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...

随机推荐

  1. flash中字体兼容性

    非谷歌浏览器设置字体font时需设置为该字体的(英文名字)或者(中文名字+可读性消除锯齿), 谷歌浏览器设置字体font时需设置为该字体的(中文名字) 总结:静态文本一般通过选择可读性消除锯齿来能实现 ...

  2. Register DLL Assembly Gacutil.exe(全局程序集缓存工具)

    全局程序集缓存工具使你可以查看和操作全局程序集缓存和下载缓存的内容. 此工具会自动随 Visual Studio 一起安装. 若要运行此工具,请使用开发人员命令提示(或 Windows 7 中的 Vi ...

  3. Dapper关联查询

    1.一对一: using (IDbConnection connecton = new MySqlConnection(ConfigurationManager.ConnectionStrings[& ...

  4. 微信App支付通知验签

    微信异步通知: [AcceptVerbs("POST")] public void Notify() { //编码(101-登录无效,102-账号无效,200-成功,201-失败, ...

  5. Java 基础之-枚举

      目录(?)[-] 用法一常量 用法二switch 用法三向枚举中添加新方法 用法四覆盖枚举的方法 用法五实现接口 用法六使用接口组织枚举 用法七关于枚举集合的使用   DK1.5引入了新的类型-- ...

  6. Valid Parentheses 使用递归的解法

    class Solution { public: bool isValid(string s) { ||s.size()%!=)return false; unordered_map<char, ...

  7. 【如何快速的开发一个完整的iOS直播app】(采集篇)

    原文转自:袁峥Seemygo    感谢分享.自我学习 前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,首先需要采集主 ...

  8. 集合类List,set,Map 的遍历方法,用法和区别

    遍历list: 方法一: for(String s:lists){ System.out.println(s); } 方法二: System.out.println("list with i ...

  9. oracle 体系结构

    oracle 体系结构 数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制. 1. oracle工作原理: 1).在数据库服务器上启动Oracle实例:2).应用程序 ...

  10. 如何更改OS系统下截图生成图片格式 jpg pdf

    Mac系统下快速截屏的快捷键: 1.截全屏: shift + command + 3 2.选取截屏 shift + command + 4 生成的图片,系统默认格式忘了,反正不好用,用下面命令更改生成 ...