美文网首页IOS开发我们爱Coding技术
iOS-基础控件-- UICollectionView简单的总结

iOS-基础控件-- UICollectionView简单的总结

作者: 云之君兮鹏 | 来源:发表于2016-05-21 21:25 被阅读886次
    **门掩黄昏,无计留春住。**<卡比兽>
    @interface ViewController ()  <UICollectionViewDataSource,UICollectionViewDelegate>
    //  对其操作遵循的两个协议
    

    @end

    @implementation ViewController
    - (void)viewDidLoad {
    [super viewDidLoad];
    //第一步:创建UICollectionViewFlowLayout 它是专门给collectionView布局

     UICollectionViewFlowLayout *flowLayout =[UICollectionViewFlowLayout new];    
    

    //第二步: 给定item的大小

     flowLayout.itemSize=CGSizeMake(60, 60);    
    

    //第三步: 设定每两个item的最小的间隔(垂直滚动)

     flowLayout.minimumInteritemSpacing=10;   
    

    //每行之间的最小间隔 (水平滚动)

      flowLayout.minimumLineSpacing=40;   
    

    //第四步: 设置滚动的方向 (垂直)

    flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;       
    

    //布局头部视图尺寸

    flowLayout.headerReferenceSize=CGSizeMake(80, 80);    
    

    //布局尾部视图尺寸

    flowLayout.footerReferenceSize=CGSizeMake(80, 80);      
    

    //视图的内边距

    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);      
    

    //布局UICollectionView

    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];       
    

    //设置代理

    collection.delegate=self;    
    collection.dataSource=self;    
    collection.backgroundColor=[UIColor redColor];    
    [self.view addSubview:collection];      
    

    //注册cell

    [collection registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];      
    

    //注册头部视图

     [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];    
    

    //注册尾部视图

     [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    

    }

    #pragma mark -------数据源 (必须实现的方法 )
    //设置分区下item数

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {   
     return 20;
    }
    

    //返回collectionView的方法

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {   
    
    FirstCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];   
    cell.photoImage.image = [UIImage imageNamed:@"3.jpg"];
    return cell;
    }
    

    //@optional
    //设置分区数

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

    //返回头部和尾部视图的样式

    -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {       
    

    //需要判断返回的是头部还是尾部视图

    if ([kind isEqualToString:UICollectionElementKindSectionHeader])     
    {        
    

    //初始化头部视图

     UICollectionReusableView *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];              
    

    //设置头部视图样式

    headerView.backgroundColor=[UIColor cyanColor];               
    return headerView;   
     }else
    {        
    UICollectionReusableView *footerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];              
     footerView.backgroundColor  = [UIColor greenColor];       
     return footerView;    
    }   
     return nil;  
     }
    

    //选择item时 ,会触发的方法

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {      
    

    //indexPath 是由 section和item 组成

    if (indexPath.item == 0 && indexPath.section == 0) {        
    SecondViewController *secondVC=[SecondViewController new];  
    点击第一区第一个单元cell跳转到  secondVC    
    [self.navigationController pushViewController:secondVC animated:YES];   
     }else if (indexPath.item == 1 && indexPath.section == 0)
    {        
    ThirdViewController *thirdVC=[ThirdViewController new];
      点击第一区第二个单元cell跳转到 thirdVC
    [self.navigationController pushViewController:thirdVC animated:YES];    
    }
    

    }

    相关文章

      网友评论

      本文标题:iOS-基础控件-- UICollectionView简单的总结

      本文链接:https://www.haomeiwen.com/subject/sprorttx.html