collectionView(手写)

来源:互联网 发布:常用端口号8000 编辑:程序博客网 时间:2024/04/29 23:24
 //

//  RootViewController.m

//  UI-day12-UICollectionView初认识

//

//  Created by liufy on 15/8/4.

//  Copyright (c) 2015 liufy. All rights reserved.

//


#import "RootViewController.h"


@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //UICollectionViewLayout是一个抽象类,里面定义了很多方法和属性,但是不一定有实现,自定义布局需要继承这个类

    

    //使用系统写好的一个瀑布流布局

    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];

    /*

     UICollectionViewScrollDirectionVertical,

     UICollectionViewScrollDirectionHorizontal

     */

    //设置滑动的方向

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    //设置item之间的最小间距(竖直滑动的时候,表示的横向间距,水平滑动的时候,表示的是纵向间距)

    layout.minimumInteritemSpacing = 35;//长的相反

    //设置item之间的最小间距(竖直滑动的时候,表示的纵向间距,水平滑动的时候,表示的是横向间距)

    layout.minimumLineSpacing = 10;//短的相同

    

    

    //实例化一个UICollectionView

    UICollectionView *collectionView = [[UICollectionView allocinitWithFrame:self.view.bounds collectionViewLayout:layout];

    

    //设置代理

    collectionView.dataSource = self;

    collectionView.delegate = self;

    

    collectionView.backgroundColor = [UIColor whiteColor];

    

    [self.view addSubview:collectionView];

    

    //注册cell(手写版本)

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

    

    //注册collectionView的头部视图

    //UICollectionReusableView 专门用于充当collectionView的头部和尾部视图

    //UICollectionElementKindSectionHeader:标识注册的是头部视图

    //UICollectionElementKindSectionFooter:标识注册的是尾部视图

    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    

}


#pragma mark- UIColloctionViewDelegate

//返回组数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    

    return 3;

    

}

//返回每组当中所有的元素个数

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

    

    return 12;

}



//返回每个元素所使用的UICollectionViewCell对象

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

    

    //去复用队列中查找cell

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

    

    //改变背景色

    cell.backgroundColor = [UIColor orangeColor];

    

    return cell;

}


//修改每一个item的宽度和高度

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

    

    return CGSizeMake(100100);

}


//返回每一组元素跟屏幕4个边界的距离(上,左,下,右)

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

    

    //创建一个UIEdgeInsets的结构体

    return UIEdgeInsetsMake(10101010);

    

}


//返回头部视图的宽和高

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    

    //注意:如果是竖直滑动,则只有高度有效,如果是水平滑动,则只有宽度有效

    return CGSizeMake(5050);

}

//返回头部和尾部视图所使用的UICollectionReusableView对象

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

    

    //由于头部和尾部视图的实例化都需要调用此方法,所以需要通过kind判断生成的是头部视图还是尾部视图

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        //表示需要创建头部视图

        //复用形式创建头部视图

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

        

        //改变背景色

        headerView.backgroundColor = [UIColor greenColor];

        

        return headerView;

        

    }

    

    return nil;

}


//选中某一个item

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

    

    NSLog(@"%ld组第%ld",indexPath.section,indexPath.item);

    

}


//效果同layout.minimumLineSpacing,二选一

//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

//    

//}


//效果同layout.minimumInteritemSpacing,二选一

//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

//    

//    

//}







@end


0 0
原创粉丝点击