#pragma mark -- 建立CollectionViewphp
- (void)createCollectionView{web
//關閉自適應ide
self.automaticallyAdjustsScrollViewInsets = NO;佈局
UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];url
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49) collectionViewLayout:fl];spa
_collectionView.dataSource = self;3d
_collectionView.delegate = self;orm
_collectionView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:0.8 ];ci
[self.view addSubview: _collectionView];rem
//佈局
fl.minimumInteritemSpacing = 0;
fl.minimumLineSpacing = 5;
//註冊cell
[_collectionView registerNib:[UINib nibWithNibName:@"RootCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"JXCell"];
[_collectionView registerNib:[UINib nibWithNibName:@"JXTeSeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"JXTSCell"];
//註冊header
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"oneHeader"];
[_collectionView registerClass:[JXHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JXHeader"];
#pragma mark -- 下拉刷新
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//下拉從新加載數據
self.page = 1;
[_dataArr removeAllObjects];
[self loadData];
}];
[header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
[header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
_collectionView.header = header;
//上拉
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//上拉加載更多
self.page += 1;
[self setMyUrl];
[self loadData];
}];
[footer setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
[footer setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
_collectionView.footer = footer;
}
#pragma mark -- dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
return 4;
}
return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
//4個特點專區
JXTeSeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JXTSCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
if (_teSeArr.count<=0) {
return cell;
}
//與滾動模型 共用
ScrollModel *model = _teSeArr[indexPath.item];
[cell loadDataFromModel:model];
return cell;
}else{
//精品推薦
RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JXCell" forIndexPath:indexPath];
if (_dataArr.count <=0) {
return cell;
}
JXJingPinModel *model = _dataArr[indexPath.item];
[cell loadDataFromJXModel:model];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
}
//
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
#pragma mark -- 一組 滾動視圖
if (indexPath.section == 0) {
UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"oneHeader" forIndexPath:indexPath];
ToAdScrollview *scrollView = [[ToAdScrollview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) andImageArr:_scrollArr];
scrollView.backgroundColor = [UIColor whiteColor];
__weak typeof (self)weakSelf = self;
scrollView.block = ^(int index){
//實現點擊某個視圖跳轉
RootWebViewController *webVC = [[RootWebViewController alloc]init];
webVC.hidesBottomBarWhenPushed = YES;
webVC.url = @"http://www.nanyibang.com/school/school.php?id=378";
[weakSelf.navigationController pushViewController:webVC animated:YES];
};
[headerView addSubview: scrollView];
return headerView;
}else{
#pragma mark -- 二組 標題
JXHeaderReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JXHeader" forIndexPath:indexPath];
return headerView;
}
}
//header高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if (section == 0) {
return CGSizeMake(SCREEN_WIDTH, 202);
}else{
return CGSizeMake(SCREEN_WIDTH, 30);
}
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return CGSizeMake((SCREEN_WIDTH - 15)/2, SCREEN_WIDTH/2 - 110);
}else{
return CGSizeMake((SCREEN_WIDTH - 15)/2, SCREEN_WIDTH/2+100);
}
}
//調節item邊距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 5, 0, 5);
}
#pragma mark -- item點擊跳轉
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}