@interface ViewController ()
設置遵從的代理
<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@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的個數多少由多因素影響, 不能人為規(guī)定(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;
// 分區(qū)內容邊間距
_flowLayout.sectionInset = UIEdgeInsetsMake(10, 75/4, 10, 75/4);
// 增廣頭視圖尺寸
_flowLayout.headerReferenceSize = CGSizeMake(30, 30);
// 增廣尾視圖尺寸
_flowLayout.footerReferenceSize = CGSizeMake(50, 50);
// 滑動方向
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
// 創(chuàng)建集合視圖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分區(qū)掷匠,第%ld個Item",indexPath.section+1,indexPath.row+1);
}