李洪强iOS开发之UICollectionView的使用

想做如下的界面效果(纯代码)

------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------

 

//

//  ViewController.m

//  A22 - 李洪强CollectionVIew的使用

//

//  Created by vic fan on 16/7/4.

//  Copyright © 2016 李洪强. All rights reserved.

//

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define SCREENH [UIScreen mainScreen].bounds.size.height

 

#import "ViewController.h"

#import "LHQHeadCellView.h"

#import "LHQPromptBuyCell.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{

    

}

/**

 *  父页面

 */

@property(nonatomic,strong)UICollectionView *collectionView;

@end

 

static NSString *const bannerId = @"bannerId";//轮播cell

static NSString *const PromptBuyId = @"PromptBuyId";//直接购买

static NSString *const fastBuyId = @"fastBuyId";//快速购买

static NSString *const dataId = @"dataID";//

 

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setUpUI];

 

}

/**

 *  创建UI

 */

- (void)setUpUI{

    /**

     *  1 创建布局对象

     */

    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];

    

    //  2 设置横向滚动

    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    /**

     *  3 初始化collectionVIew 并且设置flowlayout

     */

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:flowlayout];

    /**

     *  4 设置_collectionView的背景颜色

     */

    _collectionView.backgroundColor = [UIColor grayColor];

    /**

     *  5 设置collectionVIew的代理和数据源的方法

     */

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    /**

     *  6 添加到主页面

     */

    [self.view addSubview:_collectionView];

    /**

     *  7 collectionViewcell的注册

     */

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:bannerId];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:PromptBuyId];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:fastBuyId];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:dataId];

    //头视图和尾部视图的注册

[_collectionView registerClass:[LHQHeadCellView classforSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView"];

    

    [_collectionView registerClass:[UICollectionReusableView classforSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];

    

    

}

//----------------------------------------------------

#pragma mark  collectionView代理方法

/**

 *  代理方法1 返回几组cell

 *

 */

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 4;

}

/**

 *  代理方法2 每组有几个cell

 *

 */

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    switch (section) {

        case 0:{

            return 1;

        }

            break;

        case 1:{

            return 2;

        }

            break;

        case 2:{

            return 4;

        }

            break;

 

        case 3:{

            return 3;

        }

            break;

        default:{

            

            return 0;

        }

            break;

    }

}

/**

 *  代理方法3 每个item的大小

 */

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {

        case 0:{

            return CGSizeMake(SCREENW - 10, 150);

        }

            break;

        case 1:{

            return CGSizeMake(SCREENW/2 - 40, 150);

        }

            break;

        case 2:{

            return CGSizeMake(SCREENW / 2 - 10, 100);

        }

            break;

        case 3:{

            return CGSizeMake(SCREENW / 3 - 10, 100);

        }

            break;

            

        default:{

            return CGSizeMake(0, 0);

        }

            break;

    }

}

/**

 *  代理方法4 cell显示的内容

 *

 */

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 1) {

        LHQPromptBuyCell *cell = (LHQPromptBuyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:PromptBuyId forIndexPath:indexPath];

        cell.backgroundColor = [UIColor whiteColor];

        return cell;

    }

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:dataId forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];

    return cell;

}

/**

 *  代理方法5 补充元素的视图

    快速购买

 */

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    /**

     *  5.1先初始化reusableView

     */

    UICollectionReusableView *reusableView = nil;

    /**

     *  5.2 判断如果是头部视图

     */

    if(kind == UICollectionElementKindSectionHeader){

        /**

         *  拿到第二组和第三组

         */

        if(indexPath.section == 2|| indexPath.section == 3){

            LHQHeadCellView *headCellV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView" forIndexPath:indexPath ];

            reusableView = headCellV;

            }

        /**

         *  5.3 是尾部视图

         *

         */

    }else if (kind == UICollectionElementKindSectionFooter){

         reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

    }

    return reusableView;

    

}

 

/**

 *  代理方法6   每个item的间距

 */

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    

    return 10;

}

/**

 *  代理方法7 设置headView的大小

 */

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    

    if(section == 2 || section == 3){

        return CGSizeMake(SCREENW , 25);

    }

    return CGSizeMake(SCREENW, 0);

 

}

 

/**

 *  代理方法8 设置foot的大小

 */

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

     return CGSizeMake(self.view.frame.size.width, 10);

}

/**

 * 代理方法9 定义每个UICollectionView margin

 */

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    if (section == 1) {

        return UIEdgeInsetsMake(0, 20, 0, 20);

    }else{

        return UIEdgeInsetsMake(0, 5, 0, 5);

    }

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

原文地址:https://www.cnblogs.com/LiLihongqiang/p/5643588.html