UICollectionView

这里写图片描述

ViewController.m

#import "ViewController.h"
#import "Cell.h"
#import "UIImageView+WebCache.h"
#import "MyCollectionReusableView.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *arr;
@end

@implementation ViewController

- (void)dealloc
{
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 1.要创建collectionView,得先创建一个flowLayout,创建完之后才能创建collectionView
    // 它是苹果推荐使用的一个标准的瀑布流
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的大小,item就是tableview的row.
    flowLayout.itemSize = CGSizeMake(100, 120);
    // 设置滑动方向.
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置最小的行间距.
    flowLayout.minimumLineSpacing = 1;
    // 设置最小的列间距.
    flowLayout.minimumInteritemSpacing = 1;
    // 给item设置边框.
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);

    // 设置头部的区域范围
    flowLayout.headerReferenceSize = CGSizeMake(200, 100);
    // 底部.
//    flowLayout.footerReferenceSize = CGSizeMake(<#CGFloat width#>, <#CGFloat height#>)


    // 创建collectionView.
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [self.view addSubview:collectionV];
    [collectionV release];

    // 默认背景是黑色.
    collectionV.backgroundColor = [UIColor yellowColor];
    // 签订代理人
    collectionV.dataSource = self;
    collectionV.delegate = self;

    // collectionView想要创建cell的话只能使用注册的方式.
    [collectionV registerClass:[Cell class] forCellWithReuseIdentifier:@"reuse"];
    // 设置滑动的方向.

    // 注册头部区域.
    /**
     *  1.注册对象的类型.
        2.需要执行注册的区域是头部还是底部.
        3.重用标识.
     *
     *  @param Class <#Class description#>
     *
     *  @return <#return value description#>
     */
    [collectionV registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

    [self createData];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        MyCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor cyanColor];
        reusableView.label.text = @"1";

        // 它本身带重用的特性,所以只能注册去使用,而且注册的方法跟cell的方法不一样
        return reusableView;
    }
    return nil;
}

- (void)createData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    self.arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

}

// 必须实现的方法.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blueColor];
    cell.myLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"thumbURL"]]];

    return cell;
}
Cell.h
#import <UIKit/UIKit.h>

@interface Cell : UICollectionViewCell
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UIImageView *myImageView;
@end
#import "Cell.h"

@implementation Cell

- (void)dealloc
{
    [_myImageView release];
    [_myLabel release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [self.contentView addSubview:self.myImageView];
        [_myImageView release];

        self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 20)];
        [self.contentView addSubview:self.myLabel];
        [_myLabel release];
    }
    return self;
}
@end
MyCollectionReusableView.h
#import <UIKit/UIKit.h>

@interface MyCollectionReusableView : UICollectionReusableView

@property(nonatomic, retain)UILabel *label;
@end
#import "MyCollectionReusableView.h"
@implementation MyCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.label = [[UILabel alloc] init];
        [self addSubview:self.label];
        [_label release];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.label.frame = CGRectMake(0, 50, 100, 30);
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值