@interface ViewController ()
设置遵从的代理
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)UICollectionView *collectionView;
@end
1.UICollectionViewDelegateFlowLayout 代理方法
设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
return CGSizeMake(100, 100);
}
return CGSizeMake(90, 90);
}
设置边缘约束---上、下、左、右
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if (section==0) {
return UIEdgeInsetsMake(10, 75/4, 10, 75/4);
}
return UIEdgeInsetsMake(10, 105/4, 10, 105/4);
}
增广头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(0, 50);
}
增广尾视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(0, 30);
}
2.集合视图布局
①布局必须借助UICollectionViewFlowLayout布局类完成, UICollectionViewFlowLayout为公共类, 一般采用其子类UICollectionViewFlowLayout完成布局任务
②集合视图默认背景颜色为穿透色(黑色), 必须手动设置其背景颜色
③每行item的个数多少由多因素影响, 不能人为规定(sectionInset、itemSize、minimumLineSpacing)
④增广视图也具有代理方法, 必须存在重用机制(需注册, 可重用, 可自定义)
⑤集合视图的Cell、item子控件只有一层view,实现多功能布局必须自定义
- (void)layoutCollectionView
{
self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 属性
//item大小
_flowLayout.itemSize = CGSizeMake(100, 100);
// 最小列间距
_flowLayout.minimumInteritemSpacing = 75/4.f;
// 最小行间距
_flowLayout.minimumLineSpacing = 10.f;
// 分区内容边间距
_flowLayout.sectionInset = UIEdgeInsetsMake(10, 75/4, 10, 75/4);
// 增广头视图尺寸
_flowLayout.headerReferenceSize = CGSizeMake(30, 30);
// 增广尾视图尺寸
_flowLayout.footerReferenceSize = CGSizeMake(50, 50);
// 滑动方向
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
// 创建集合视图CollectionView
- (void)creatCollectionView
{
self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_flowLayout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
// 设置背景颜色
_collectionView.backgroundColor = [UIColor cyanColor];
// 注册Cell/item
// [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
[_collectionView registerNib:[UINib nibWithNibName:@"ApCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ApCollectionViewCell"];
// 注册增广头视图
// [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
[_collectionView registerNib:[UINib nibWithNibName:@"ApReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ApReusableView"];
// 注册增广尾视图
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
self.view = _collectionView;
}
3.collectionView相关代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section==0) {
return 9;
}else{
return 21;
}
// return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
ApCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ApCollectionViewCell" forIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.f];
return cell;
}
// 增广视图代理方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
ApReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ApReusableView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor redColor];
return headerView;
}else{
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor greenColor];
return footerView;
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"第%ld分区,第%ld个Item",indexPath.section+1,indexPath.row+1);
}