UICollectionView入门级使用

这两天没事撸撸代码,自己提升一下 就想重构公司的代码  后来想想太多了  就先重构一个界面吧   然后就写了一个CollectionView做的界面  其中遇到的难点就是设置它的headerView然后让他的headerView悬浮于不悬浮的问题

我们先来看看 CollectionView的初始化吧


    self.firstView = [[FirstView alloc] initWithFrame:CGRectMake(0, 0, W, H/3)];
    

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//    flowLayout.itemSize = CGSizeMake(50, 50);
    flowLayout.minimumInteritemSpacing = 20;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    /**
     *  这个非常重要   设置headerView和collectionVirw的层级关系的   也就是设置headerView是否悬浮,
     */
    
    [flowLayout setSectionHeadersPinToVisibleBounds:YES];
    
    /**
     *  这个设置headerView的大小的,W(宽度)感觉写与不写没什么关系的  好像默认的是屏幕的宽度
     */
    [flowLayout setHeaderReferenceSize:CGSizeMake(W, 200)];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  H - 64) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor grayColor];
    self.collectionView.showsVerticalScrollIndicator = NO;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.scrollView addSubview:self.collectionView];

这里感觉collectionView的headerView和tableView的cell差不多,要注册,然后配合这个方法实现

    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"asd"];

//设置headerView的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    _headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"asd" forIndexPath:indexPath];

    if (indexPath.section == 1) {
        _headerView.backgroundColor = [UIColor yellowColor];
    }else{
        _headerView.backgroundColor = [UIColor redColor];
    }
//    [collectionView addSubview:_headerView];

//    self.firstView   就是自己定义的headerView   这个如果只需要一个的话  最好提前创建好  不要在这里面创建   否则没走一次方法就会创建一次  容易造成内存负担

    [_headerView addSubview:self.firstView];
    return _headerView;
    
}

其余的代理方法就跟正常的一样

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 120;
}
//指定每一个Item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row%2) {
//        return CGSizeMake(180, 180);
//    }else
        return CGSizeMake((W - 60)/2, 260);
    
//}
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * string = @"cell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}


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

好吧   我就看了这门多 毕竟我是入门级高手  O(∩_∩)O哈哈~    如有不对之处   还请大神不吝赐教

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值