iOS UICollectionView

@interface RootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

// UICollectionViewDelegateFlowLayout 点进去发现, 该类遵守了UICollectionViewDelegate协议
// 实际上, UICollectionViewDelegateFlowLayout这个协议是UICollectionViewDelegate的子协议

@property (nonatomic, retain) UIImageView *imageView;

@end

@implementation RootViewController

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imageView = [[[UIImageView alloc] init] autorelease];
    imageView.frame = CGRectMake(0, 0, 150, 200);
    [self.view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"1"];
    self.imageView = imageView;
    [self addSubViews];
    
}


// 创建一个集合视图
- (void)addSubViews
{
    // Item布局 (网格状布局)
    // 行边距(相对于上下滑) 如果左右滑动就是列边距
    // 页边距(相对于左右滑) 如果左右划 就是行边距
    // UICollectionViewLayout 是一个抽象类, 其功能是有其子类来实现的, 本身没有具体功能
    UICollectionViewFlowLayout *layOut = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layOut.minimumLineSpacing = 30;
    layOut.minimumInteritemSpacing = 30;
    layOut.itemSize = CGSizeMake(150, 200); // 设置大小
    layOut.scrollDirection = UICollectionViewScrollDirectionVertical; // 设置滑动方向(默认是上下滑动的)
    
    // 设置表头, 只有高度影响表头表尾, 宽度没用的
    layOut.headerReferenceSize = CGSizeMake(0, 100);
    
    // 设置表尾
    layOut.footerReferenceSize = CGSizeMake(0, 100);
    
    // 设置内边距
    layOut.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    
    
    
    
    
    // 初始化集合视图
    UICollectionView *collectionView = [[[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layOut] autorelease];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    
    // 显示视图
    [self.view addSubview:collectionView];
    
    // 注册要用的cell
    // Identifier重用的标示符要一致
    // cell是哪个类, Class就填哪个类, 使用系统就注册系统的, 如果自定义的话, 就注册自定义的
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    // 注册表头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];
    
    // 注册表尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter"];
    
    
}

#pragma mark -- 必须实现的方法
// 必须实现的两个方法跟tableView一样
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回每个item的方法
    // 这个方法里面包括了创建tableViewCell写的一堆(复用)
    // 必须有一步, 必须要注册cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    // 系统没有像tableView一样提供布局方式, 要使用UICollectionViewCell一般都使用自定义cell, 跟tableView一样, 所有的自定义控件都要加载contectView上面
    cell.contentView.backgroundColor = [UIColor greenColor];
    
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 200)] autorelease];
    imageView.image = [UIImage imageNamed:@"1"];
    
    [cell.contentView addSubview:imageView];
    return cell;
}

// 返回分区数, 跟tableView一样, 默认就1个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 设置表头表尾, 通过代理方法来实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 判断返回表头还是表尾
    // 应为参数是: 字符串的, 判断相同不能用等号
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 返回表头, 需要去复用集合中得到
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor greenColor];
        return headerView;
    } else {
        // 返回表尾
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor yellowColor];
        return footerView;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值