collectionView纯代码使用

collectionView和tableView最大的不同之处就是需要自定义cell,所以第一步自定义collectionViewCell

1.自定义collection 


// 初始化 layout
   
UICollectionViewFlowLayout *flowLayout =[[ UICollectionViewFlowLayout alloc ] init ];
   
// 2. 设置每个格子的尺寸
    flowLayout.
itemSize = CGSizeMake ( 90 , 90 );
   
   
// 3. 设置整个 collectionView 的内边距
   
   
CGFloat paddingY = 20 ;
   
   
CGFloat paddingX = 20 ;
   
    flowLayout.
sectionInset = UIEdgeInsetsMake (paddingY, paddingX, paddingY, paddingX);
   
   
// 4. 设置每一行之间的间距
    flowLayout.
minimumLineSpacing = paddingY;
//    flowLayout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 50);// 设置 headerView 的尺寸大小
    [flowLayout
setScrollDirection : UICollectionViewScrollDirectionVertical ]; // 设置滚动方向
   
// 初始化 ConllectionView
   
_commonConllectionView = [[ UICollectionView alloc ] initWithFrame : CGRectMake ( 0 , 0 , SCREENWIDTH , SCREENHEIGHT ) collectionViewLayout :flowLayout];
   
// 注册单元格
    [
_commonConllectionView registerClass :[ WCRCommonCollectionViewCell class ] forCellWithReuseIdentifier : @"commonCollectionCell" ];
//    _commonConllectionView
    //注册headerView 此处的ReuserIdentifier 必须和 cellForItemAtIndexPath 方法中一致  
    [ _commonConllectionView registerClass:[*** class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withTeuseIdentifier @“reuableView”];
   
   
// 设置代理
   
_commonConllectionView . delegate = self ;
   
_commonConllectionView . dataSource = self ;
   
_commonConllectionView . backgroundColor = [ UIColor whiteColor ];
    [self.view addSubview:_commonConllectionView];

2.实现3个代理 DataSource,Delegate,DelegateFlowLayout

3.实现代理方法
-( NSInteger )numberOfSectionsInCollectionView:( UICollectionView *)collectionView
{
   
return 1 ;
}

-(
NSInteger )collectionView:( UICollectionView *)collectionView numberOfItemsInSection:( NSInteger )section{
   
return _commonConllectionArray . count ;
}

// 定义每个 UICollectionView 的大小
- (
CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    return CGSizeMake ( 100 , 100 );
}

-(
UICollectionViewCell *)collectionView:( UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath{
   
    WCRCommonCollectionViewCell *cell = (WCRCommonCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"commonCollectionCell" forIndexPath:indexPath];

    cell. commonInfo = _commonConllectionArray [indexPath. row ];
   
return cell;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值