UIcollection基础

//创建布局
    UICollectionViewFlowLayout * danPinFlowLayout =[[UICollectionViewFlowLayout alloc]init];
    danPinFlowLayout.itemSize= CGSizeMake(230, 240);
    danPinFlowLayout.headerReferenceSize=CGSizeMake(0, 50);
    danPinFlowLayout.footerReferenceSize =CGSizeMake(0, 50);
    danPinFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//竖向滚动
    //初始化collectionView
    CGRect collectRect=CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    if(_DanPinCollectionView ==nil){
        _DanPinCollectionView =[[UICollectionView alloc]initWithFrame:collectRect collectionViewLayout:danPinFlowLayout];
        _DanPinCollectionView.tag=200;
        _DanPinCollectionView.backgroundColor=[UIColor whiteColor];
        _DanPinCollectionView.dataSource=self;
        _DanPinCollectionView.delegate=self;
        _DanPinCollectionView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_DanPinCollectionView];
        //注册cell
        UINib *nibCell = [UINib nibWithNibName:@"DanPinCollectionViewCell"
                                        bundle: [NSBundle mainBundle]];
        [_DanPinCollectionView registerNib:nibCell forCellWithReuseIdentifier:@"danPinCellIdentifier"];
        //headerView注册
        [_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderIdentifierhead"];
        //footView注册
        [_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FootIdentifierhead"];
    }

}

  cell模板建议使用xib文件直接创建,更快捷方便。

  基础协议实现

#pragma mark ----------------UIcollectionViewDataSource---------------
//组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;//self.DanPinDataSource.count;
}

//构建单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"danPinCellIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

//组的头视图创建
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if(kind ==UICollectionElementKindSectionHeader){
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderIdentifierhead" forIndexPath:indexPath];
        headView.backgroundColor = [UIColor blueColor];
        return headView;
    }
    else{
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FootIdentifierhead" forIndexPath:indexPath];
        footView.backgroundColor = [UIColor redColor];
        return footView;
    }
}

//通过协议方法设置单元格尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CGFloat rd = arc4random()%200;
    return CGSizeMake(220, 240);
}


转载于:https://my.oschina.net/u/1776190/blog/598951

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值