UICollectionView简单使用


//
//  CFHome2Controller.m
//  CFLottery
//
//  Created by GongCF on 2017/8/7.
//  Copyright © 2017年 GongCF. All rights reserved.
//

#import "CFHome2Controller.h"
#import "CFCycleBannerView.h"
#import "CFBannerModel.h"
#import "BaseWebViewController.h"
#import "CFHomeCollectionCell1.h"
#import "CFHomeCollectionCell2.h"
#import "CFHomeCollectionCell3.h"
#import "CFGameModel.h"
static NSString *cellId1=@"cellId1";
static NSString *cellId2=@"cellId2";
static NSString *cellId3=@"cellId3";
static NSString *headerId1=@"headerId1";
static NSString *headerId2=@"headerId2";
static NSString *footerId1=@"footerId1";
@interface CFHome2Controller ()
{
    NSMutableArray *_gameArr;
    NSMutableArray *_hotSoccerGameArr;
    NSMutableArray *_hotBasketGameArr;

}
@property (weak, nonatomic) IBOutlet UIView *infoView;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation CFHome2Controller

- (void)viewDidLoad {
    [super viewDidLoad];
     _gameArr=[[NSMutableArray alloc]init];
    _hotBasketGameArr=[[NSMutableArray alloc]init];
    _hotSoccerGameArr=[[NSMutableArray alloc]init];
    //数据
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //游戏信息
        [self loadGameInfo];
        //热门赛事
        [self loadHotGame];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView.collectionViewLayout=flowLayout;

    // 注册cell、sectionHeader、sectionFooter
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId1];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId2];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId1];
}

#pragma mark - HTTP
- (void)loadGameInfo
{
    [HTTPManger POST:gameInfoUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        [self.collectionView.mj_header endRefreshing];
        if ([responseObject[@"code"] integerValue]==1)
        {
            _gameArr=[[NSMutableArray alloc]init];
            for (NSDictionary *dic in responseObject[@"data"]) {
                CFGameModel *model=[[CFGameModel alloc]initWithDataDic:dic];
                [_gameArr addObject:model];
            }
            [self.collectionView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}

- (void)loadHotGame
{
    [HTTPManger POST:hotGameUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        [self.collectionView.mj_header endRefreshing];
        if ([responseObject[@"code"] integerValue]==1)
        {
            if ([responseObject[@"data"][0][@"football"] count]>0) {
                _hotSoccerGameArr=[[NSMutableArray alloc]init];
                [_hotSoccerGameArr addObjectsFromArray:responseObject[@"data"][0][@"football"]];
            }
            
            //            if ([responseObject[@"data"][1][@"basketball"] count]>0) {
            _hotBasketGameArr=[[NSMutableArray alloc]init];
            //                [_hotBasketGameArr addObjectsFromArray:responseObject[@"data"][1][@"basketball"]];
            //            }
            [self.collectionView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}


#pragma mark ---- UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
     if (section==0) {
         return _gameArr.count;
     }else if (section==1)
     {
         return _hotSoccerGameArr.count+_hotBasketGameArr.count;
     }else
     {
         return 0;
     }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        CFHomeCollectionCell1 *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId1 forIndexPath:indexPath];
        cell.model=_gameArr[indexPath.item];
        return cell;
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row<_hotSoccerGameArr.count)
        {
            CFHomeCollectionCell2 *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId2 forIndexPath:indexPath];
            cell.dataDic=_hotSoccerGameArr[indexPath.item];
            cell.orderBtnBlock = ^{
                [self performSegueWithIdentifier:@"lottery://football" sender:nil];
            };
            return cell;
        }else
        {
            CFHomeCollectionCell3 *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId3 forIndexPath:indexPath];
            cell.dataDic=_hotBasketGameArr[indexPath.item-_hotSoccerGameArr.count];
            cell.orderBtnBlock = ^{
                [self performSegueWithIdentifier:@"lottery://basketball" sender:nil];
            };
            return cell;
        }
    }else
    {
        return nil;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        if (indexPath.section==0) {
            UICollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId1 forIndexPath:indexPath];
            if (headerView.subviews.count<=0) {
                [headerView addSubview:self.infoView];
            }
            return headerView;
        }else if (indexPath.section==1)
        {
            UICollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId2 forIndexPath:indexPath];
            if (headerView.subviews.count<=0) {
                headerView.backgroundColor=UIColorFromRGB(0XF5F5F5, 1);
                UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 30)];
                leftView.backgroundColor=UIColorFromRGB(0Xe0271d, 1);
                [headerView addSubview:leftView];
                UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 150, 30)];
                lbl.font=[UIFont systemFontOfSize:12];
                lbl.textColor=UIColorFromRGB(0Xe0271d, 1);
                lbl.text=@"热门赛事";
                [headerView addSubview:lbl];
            }
            return headerView;

        }
    }else{
        //必须得写不然报错
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId1 forIndexPath:indexPath];
        return footer;
    }
    return nil;
}


#pragma mark - UICollectionViewDelegateFlowLayout
//section的上左下右间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return (CGSize){kScreenWidth/2.0-0.5, 100};
    }else if (indexPath.section==1)
    {
        return (CGSize){kScreenWidth, 110};
    }
    return (CGSize){0, 0};
}

//Section的Header大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return (CGSize){kScreenWidth,self.view.width*(175.0/375.0)+30};
    }else if(section==1)
    {
        return (CGSize){kScreenWidth,30};
    }else
    {
        return (CGSize){0,0};
    }
}

//Section的Footer大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return (CGSize){kScreenWidth,0.1};
}

//item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if (section==1)
    {
        return 15;
    }else
    {
        return 0;
    }
}

//item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if (section==1)
    {
        return 0;
    }else
    {
        return 0;
    }
}

#pragma mark - UICollectionViewDelegate
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        CFGameModel *model=_gameArr[indexPath.item];
        [self performSegueWithIdentifier:model.schemeUrl sender:nil];
    }
}


@end

你可能感兴趣的:(UICollectionView简单使用)