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 ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,888评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,677评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,386评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,726评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,729评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,337评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,902评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,807评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,349评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,439评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,567评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,242评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,933评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,420评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,531评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,995评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,585评论 2 359

推荐阅读更多精彩内容