UICollectionView简单使用

@interface XCZCollectionHeader : UICollectionReusableView布局



#import "XCZCollectionsViewController.h"spa

#import "XCZCollectionViewCell.h".net

#import "XCZCollectionHeader.h"代理


@interface XCZCollectionsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>orm


@end 对象


@implementation XCZCollectionsViewControllerip


- (void)viewDidLoad {ci

    [super viewDidLoad];get

    // Do any additional setup after loading the view.it

    self.title = @"分类";

     [self createCollectionView];

}


- (void)createCollectionView {

 // 第一步:新建布局对象

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];

    CGFloat padding = 10;

    CGFloat itemW = (self.view.frame.size.width - 5 * padding)/4;

    CGFloat itemH = itemW;

    layout.itemSize = CGSizeMake(itemW, itemH);

    

    // 头尾高度

    layout.headerReferenceSize = CGSizeMake(0, 10);

    // 最小间隔

    // 对于垂直滚动,它是行间距

    // 对于水平滚动,它是列间距


    layout.minimumLineSpacing = 10;

    // 内边距

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


    

    // 集合视图,用于块状分布

    // iOS6.0以后出现的视图

    // 第二步,新建集合视图,关联布局对象

    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    

    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height - 64) collectionViewLayout:layout];

    

    // 第三步,注册cell, 注册追加视图(section的头和尾)

    // 注册cell

    [collectionView registerClass:[XCZCollectionViewCell class] forCellWithReuseIdentifier:@"MyCellID"]; // 复用标识,可随意写

    

    // 注册supplementary view

    // 第一个参数:注册的类

    // 第二个参数:追加视图的类型 UICollectionElementKindSectionHeader  表示头,UICollectionElementKindSectionFooter表示尾 至关于tableView 中分区的headerViewfooterView

    // 第二个参数:复用标识

    [collectionView registerClass:[XCZCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MySectionHeaderViewID"]; // 注册头

    

    // 第四步,设置代理,并实现代理方法

    collectionView.delegate = self;

    collectionView.dataSource = self;

    collectionView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:collectionView];


}


#pragma mark - 实现代理方法

// 多少个分区

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

    return 4;

}


// 每一个分区有多少cell(item)

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

    if (section == 0) {

        return 7;

    }else if (section == 1){

    

        return 11;

    }else if (section == 2){

    

        return 12;

    

    }else if (section == 3){

    

        return 7;

    }

    return section;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


// 每一个cell是什么

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

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

    cell.imageView.image = [UIImage imageNamed:@"1"];

    cell.textLabel.text = @"嘎嘎";

    return cell;

}


// 头尾视图

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

    // 根据是头视图仍是尾视图做判断

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

    headerView.backgroundColor = [UIColor grayColor];

 

   NSArray * arr = @[@"选集",@"主题",@"写景",@"节日"];

//    NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group #%li",indexPath.section +1];


    headerView.textLabel.text = arr[indexPath.section];


    

    

    

    return headerView;

}


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


    NSLog(@"--%ld",(long)indexPath.row);



}

// 经过代理方法设置headerView的大小

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

    return CGSizeMake(0, 26);

}

相关文章
相关标签/搜索