//
// 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
UICollectionView簡單使用
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內容
- 一、設置 UICollectionViewFlowLayout 二、設置collectionView 三、coll...
- UICollectionView 是沒有類似于UITableview的整體表頭的,在注冊cell時,如果分區(qū)表頭,...