UI第十四天
2016-03-19 10:46
671 查看
注意:
由于collectionV背景默认为黑,一般我们给他一个背景颜色
collectionV.backgroundColor = [UIColor
whiteColor];
一些概念:
UICollectionView
1.遵循协议
<UICollectionViewDelegate,UICollectionViewDataSource>
2. //创建流布局
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout
alloc]
init];
CGFloat itemWH = (self.view.frame.size.width -
40)/3;
//设置cell的大小
flow.itemSize =
CGSizeMake(itemWH, itemWH);
//在纵向(默认滑动方向)设置的是左右cell之间的最小距离
//在横向
设置的是上下cell之间的最小距离
flow.minimumInteritemSpacing =
0;
//在纵向(默认滑动方向)设置的是上下cell之间的最小距离
//在横向
设置的是左右cell之间的最小距离
flow.minimumLineSpacing =
10;
//设置分组距离collectionView四周的边距
flow.sectionInset =
UIEdgeInsetsMake(20,
10, 10,
10);
//设置头部视图的宽高
flow.headerReferenceSize =
CGSizeMake(self.view.frame.size.width,
40);
UICollectionView *collectionV = [[UICollectionView
alloc] initWithFrame:self.view.bounds
collectionViewLayout:flow];
collectionV.tag =
1000;
collectionV.delegate =
self;
collectionV.dataSource =
self;
//背景默认为黑
collectionV.backgroundColor = [UIColor
whiteColor];
[self.view
addSubview:collectionV];
//注册cell
[collectionV registerClass:[CustomCollectionViewCell
class] forCellWithReuseIdentifier:@"cell"];
//注册头部复用
//UICollectionElementKindSectionHeader
标记头部
//UICollectionElementkindSectionFooter
标记尾部
[collectionV registerClass:[HeaderCollectionReusableView
class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"header"];
3.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
//从复用池中取cell,如果没有取到,那么系统会自动给你创建cell
CustomCollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
NSDictionary *dic =
self.dataArray[indexPath.row];
cell.imageV.image = [UIImage
imageNamed:dic[@"userImage"]];
cell.layer.borderWidth =
1.0;
cell.layer.borderColor = [[UIColor
blackColor]CGColor];
cell.titleLabel.text = dic[@"userName"];
cell.titleLabel.textAlignment =
NSTextAlignmentCenter;
return cell;
}
4.
#pragma mark ---头部尾部视图复用协议函数
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind
isEqualToString:UICollectionElementKindSectionHeader])
{
HeaderCollectionReusableView *header = [collectionView
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"header"
forIndexPath:indexPath];
header.backgroundColor = [UIColor
redColor];
return header;
}
else
{
return nil;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath
{
NSLog(@"section:%ld-----row:%ld",indexPath.section,indexPath.row);
}
由于collectionV背景默认为黑,一般我们给他一个背景颜色
collectionV.backgroundColor = [UIColor
whiteColor];
一些概念:
UICollectionView
1.遵循协议
<UICollectionViewDelegate,UICollectionViewDataSource>
2. //创建流布局
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout
alloc]
init];
CGFloat itemWH = (self.view.frame.size.width -
40)/3;
//设置cell的大小
flow.itemSize =
CGSizeMake(itemWH, itemWH);
//在纵向(默认滑动方向)设置的是左右cell之间的最小距离
//在横向
设置的是上下cell之间的最小距离
flow.minimumInteritemSpacing =
0;
//在纵向(默认滑动方向)设置的是上下cell之间的最小距离
//在横向
设置的是左右cell之间的最小距离
flow.minimumLineSpacing =
10;
//设置分组距离collectionView四周的边距
flow.sectionInset =
UIEdgeInsetsMake(20,
10, 10,
10);
//设置头部视图的宽高
flow.headerReferenceSize =
CGSizeMake(self.view.frame.size.width,
40);
UICollectionView *collectionV = [[UICollectionView
alloc] initWithFrame:self.view.bounds
collectionViewLayout:flow];
collectionV.tag =
1000;
collectionV.delegate =
self;
collectionV.dataSource =
self;
//背景默认为黑
collectionV.backgroundColor = [UIColor
whiteColor];
[self.view
addSubview:collectionV];
//注册cell
[collectionV registerClass:[CustomCollectionViewCell
class] forCellWithReuseIdentifier:@"cell"];
//注册头部复用
//UICollectionElementKindSectionHeader
标记头部
//UICollectionElementkindSectionFooter
标记尾部
[collectionV registerClass:[HeaderCollectionReusableView
class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"header"];
3.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
//从复用池中取cell,如果没有取到,那么系统会自动给你创建cell
CustomCollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
NSDictionary *dic =
self.dataArray[indexPath.row];
cell.imageV.image = [UIImage
imageNamed:dic[@"userImage"]];
cell.layer.borderWidth =
1.0;
cell.layer.borderColor = [[UIColor
blackColor]CGColor];
cell.titleLabel.text = dic[@"userName"];
cell.titleLabel.textAlignment =
NSTextAlignmentCenter;
return cell;
}
4.
#pragma mark ---头部尾部视图复用协议函数
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind
isEqualToString:UICollectionElementKindSectionHeader])
{
HeaderCollectionReusableView *header = [collectionView
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"header"
forIndexPath:indexPath];
header.backgroundColor = [UIColor
redColor];
return header;
}
else
{
return nil;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath
{
NSLog(@"section:%ld-----row:%ld",indexPath.section,indexPath.row);
}
相关文章推荐
- UI第十三天
- CF 450 B Jzzhu and Sequences(矩阵快速幂)
- pair类型 这次遇到了,记录下来,方便彼此xue习
- UI第十二天
- UI第十一天
- Java程序StringBuilder的效率,验证字符串操作和字符串生成器的操作效率
- easyUI接收后台的参数不正确导致报length no defined
- Starting MySQL... ERROR! The server quit without updating PID file (/mydata/data/t1.pid).
- js构建ui的统一异常处理方案(一)
- IOS UICollectionView基础+UICollectionViewFlowLayout基础
- 【图像处理】【SEED-VPM】5.uImage的烧写 & NFS烧写文件系统
- 在fragment 传递数据的时候报:Parcel: unable to marshal value
- js构建ui的统一异常处理方案(一)
- ElasticSearch之QueryBuilders
- ElasticSearch之QueryBuilders
- 《Concurrency Programming Guide》 笔记
- iOS开发基础控件--按钮(UIButton)
- 187. Repeated DNA Sequences
- 扣丁学堂笔记第04天基础UI组件(二)
- [leetcode] 255. Verify Preorder Sequence in Binary Search Tree 解题报告