collectionview flowLayout使用

 

// 初始化layout           

    UICollectionViewFlowLayout *fowLayout = [[UICollectionViewFlowLayout alloc] init];
     //设定全局的行间距,如果想要设定指定区内Cell的最小行距,可以使用下面方法:
     fowLayout.minimumLineSpacing = 10;
     //设定全局的Cell间距,如果想要设定指定区内Cell的最小间距,可以使用下面方法:
     fowLayout.minimumInteritemSpacing = 10;
     //itme上左下右的间距
     fowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
     //设定滚动方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal两个值。
     fowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
     //设置item大小
     fowLayout.itemSize = CGSizeMake(80.0 , 80.0);

       UICollectionView * collectionView = [[UICollectionViewalloc] initWithFrame:CGRectMake(0,0, 320, 100)collectionViewLayout:flowLayout];


//注册单元格    

//1、正常方式

[collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:identifier]; 

//2、自定义方式

[collectionView registerClass:[VideoCell class]forCellWithReuseIdentifier:@"cell"]; 

//3、xib自定义方式

[collectionViewregisterNib:[MyCellnib]forCellWithReuseIdentifier:@"MyCell"];  


collectionView.backgroundColor = [UIColor whiteColor];

        collectionView.delegate =self;

        collectionView.dataSource =self;

        [self.viewaddSubview:collectionView];



--------------委托代理【与tableview类似】--------------

#pragma mark - collectionView delegate

//设置分区

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

//每个分区上得元素个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 24;

}


//设置元素内容

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

  static NSString *identify = @"cell";

    UICollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

    [cell sizeToFit];

    cell.backgroundColor =[UIColor greenColor];


//自定义

    VideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

    [cell sizeToFit];

    if (!cell) {

        

    }

    VideoModel *model = [self.videoModelsobjectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:model.videoImgURL];

    

    [cell.imgView setImageWithURL:url];

    cell.titleLbale.text = model.videoTitle;


    return cell;

}


//设置元素的的大小框【如果上面fowLayout已经设置可以不实现以下方法

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    UIEdgeInsets top = {30,15,30,15};

    return top;

}


//设置元素大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(140,160);

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值