CollectionView Gird布局及基本使用

CollectionViewOS6之后出现的视图控件,与UITableView类似,最大的特点是可以对cell实现灵活的布局

一UICollectionViewFlowLayout 介绍
介绍:UICollectionViewFlowLayout系统提供的,能够实现cell的网格布局

UICollectionViewFlowLayout的父类是UICollectionViewLayout,UICollectionViewLayout集合视图的布局类,是一个抽象基类

二.使用

    UICollectionViewFlowLayout *layot = [[UICollectionViewFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,80, self.view.frame.size.width,self.view.frame.size.height-80) collectionViewLayout:layot];
    
    CGFloat itemW = (self.view.frame.size.width-20)/3;
    
    CGFloat itemH = itemW;
    collectionView.backgroundColor = [UIColor redColor];
    layot.itemSize = CGSizeMake(itemW, itemH);
    //调整item的内边距
     layot.sectionInset = UIEdgeInsetsMake(5,5, 5, 5);
    //滚动方向
    layot.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置最小行间距 竖直滚动横向 ,注意如果你设置了该属性为0时cell的行间距还是有间隔的话,有可能是图片的contentModel填充方式不对
    layot.minimumLineSpacing =5;
    
    //竖直滚动,垂直方向 该属性根据屏幕宽度可以放几根cell剩余的宽度来配置,当cellw款度设定了每行3个那么他们之间的间距是没有的在设置该属性时就会出现拍版错误
     layot.minimumInteritemSpacing =0;
    
    
   
    //头部视图的宽高
    layot.headerReferenceSize = CGSizeMake(self.view.frame.size.width,300);
    //尾部视图的宽高
   // layot.footerReferenceSize
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    
    [self.view addSubview:collectionView];
    
    
    
    //9.0之后只要设置该属性就可以实现collectionview的头部视图悬停效果
  //  layot.sectionHeadersPinToVisibleBounds = YES;

三.代理方法

#pragma mark---必须要实现datasoucer
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 10;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return array.count;
    
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:array[indexPath.row]];
    cell.imageView.contentMode = UIViewContentModeScaleToFill;
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}




-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了%ld",indexPath.row);
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值