UICollectionView的基本使用 collectionView

#pragma mark -- 创建CollectionView

- (void)createCollectionView{

    //关闭自适应

    self.automaticallyAdjustsScrollViewInsets = NO;

    

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

    

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49) collectionViewLayout:fl];

    

    _collectionView.dataSource = self;

    _collectionView.delegate = self;

    _collectionView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:0.8 ];

    

    [self.view addSubview: _collectionView];

    //布局

    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{

    

    

}

原文地址:https://www.cnblogs.com/daxueshan/p/6742867.html