您的位置:首页 > 产品设计 > UI/UE

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: