UICollectionView(集合视图)以及自定义集合视图
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;
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;
}
}
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;
UICollectionView *_collectionView;
[_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
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;
CollectionViewCell的类为
MyCollectionViewCell
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:_imageView];
}
return self;
UICollectionView(集合视图)以及自定义集合视图的更多相关文章
- UICollectionView 集合视图用法,自定义Cell
在View里面 //1.创建UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFl ...
- springMVC源码解析--ViewResolverComposite视图解析器集合(二)
上一篇博客springMVC源码分析--ViewResolver视图解析器(一)中我们介绍了一些springMVC提供的很多视图解析器ViewResolver,在开发的一套springMVC系统中是可 ...
- 集合、拆箱、装箱、自定义集合的foreach
集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...
- 《C#本质论》读书笔记(16)构建自定义集合
16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...
- 使用yield关键字让自定义集合实现foreach遍历
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...
- [c#基础]集合foreach的必要条件和自定义集合
引言 最近翻看了之前的学习笔记,看到foreach,记得当时老师讲的时候,有点犯浑,不是很明白,这好比,上小学时,你不会乘法口诀,但是随着时间的增长,你不自觉的都会了,也悟出个小道理,有些东西,你当时 ...
- Qt之模型/视图(自定义进度条)
简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...
- Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果
开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...
随机推荐
- erlang,elixir安装
erlang下载地址:https://packages.erlang-solutions.com/erlang/ elixir(precompile版)下载地址:https://github.com/ ...
- jQuery Mobile 中创建按钮
在 jQuery Mobile 中创建按钮 jQuery Mobile 中的按钮可通过三种方法创建: 使用 <button> 元素 使用 <input> 元素 使用 data- ...
- curl 模拟登录微信公众平台带验证码
这段时间一直写个项目, 从切图到前端到后台都要搞定,真tm累. 今天下午手残,不停用错误的密码去模拟登录微信公众平台,结果后来出现验证码,瞬间悲剧(菜鸟从来没搞过带验证码的). 研究了一下,发现其实很 ...
- KinectV2+Ubuntu 14.04+Ros 安装教程
前言 个人理解错误的地方还请不吝赐教,转载请标明出处,内容如有改动更新,请看原博:http://www.cnblogs.com/hitcm/ 如有任何问题,feel free to contact m ...
- typedef 与指针、多维数组
1.在typedef中使用指针往往会带来意外的结果.如下: typedef string *pstring; const pstring cstr; 绝大数人刚开始都会认为cstr是一种指针,它指向c ...
- Linux字符串截取和处理命令 cut、printf、awk、sed、sort、wc
1. cut [选项] 文件名 -f 列号 #提取第几列(分隔符默认为\t) -d 分隔符 #指定分隔符 例如:cut -f 2 a.txt #截取文件a.txt内容的第二列(列号从1开始) cu ...
- 使用kindeditor文本编辑器
aspx中代码: <%@ Page Language="C#" ValidateRequest="false" AutoEventWireup=" ...
- xml的解析与创建——bing到youdao导入文件的转换
首先是为了解决一个问题:如何将必应单词本中记录的单词转入到有道词典中去.实际上,必应词典可以导出xml文件,但是该文件有道词典无法解析.这里涉及到xml的解析和创建了. 代码如下: import ja ...
- free-electrons linux内核和驱动
操作系统的三个作用:1.管理硬件资源:2.提供独立于架构和硬件的可移植的软件接口3.处理不同应用对相同硬件资源的同时访问 系统调用接口是稳定的,系统调用由c函数库封装,用户程序基本不需要直接调用系统函 ...
- 排序陷阱 List.Sort Linq.OrderBy
部分内容摘自:http://www.th7.cn/Program/net/201511/692766.shtml C#框架里面主要提供了两种排序方式:Array.Sort Linq.Orderby. ...