iOS激情详解之集合视图

//  RootViewController.m
//  UICollectionView

#import "RootViewController.h"

@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
// 3,UICollectionViewDelegateFlowLayout 点进去发现该类遵守了UICollectionViewDelegate

// 实际上UICollectionViewDelegateFlowLayout这个协议是UICollectionViewDelegate的子协议

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addSubViews];
}

// 集合视图
- (void)addSubViews
{
    //UICollectionViewLayout是一个抽象类,其功能本身没有具体的功能,其功能是由他的子类实现的:UICollectionViewFlowLayout
    // 2,Item 布局 (网格状布局)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 6,行边距(相对于 上下滑动) 如果左右滑动,就是列边距
    layout.minimumLineSpacing = 30;
    // 7,列边距(相对于 上下滑动) 如果左右滑动,就是行边距
    layout.minimumInteritemSpacing = 30;
    // 8,设置item的宽高
    layout.itemSize = CGSizeMake(80, 80);
    // 9, 设置滑动方向(默认上下滑动)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 10,设置表头
    layout.headerReferenceSize = CGSizeMake(100, 200);
    // 11,设置表尾
    layout.footerReferenceSize = CGSizeMake(50, 100);
    // 12,设置内边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    // 1,初始化集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    // 4,设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;

    // 默认是黑的
    collectionView.backgroundColor = [UIColor purpleColor];

    // 5,显示视图
    [self.view addSubview:collectionView];
    [collectionView release];
    [layout release];

    //9,注册 你要用的cell
    // Identifier:@"MyCell" 重用标识符一定要一致
    // (Class) 你的cell是哪一类的就填写哪个类的 使用的是系统的话,就注册系统的,如果是自定义的话,就注册自定义的
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
}

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

// 必须实现的两个方法
// 6,返回每个分区的Item数目
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
// 7,返回每一个Item的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //返回每一个Item的方法
    // 这个方法里面包括了咱们之前tableViewCell写的一堆
    // 必须要有一步:注册cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    // 12,注册表头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];

    // 13,注册表尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter"];

    // 系统没有像tableView一样给咱们给他们提供布局方式,
    // 咱们要使用UICollectionViewCell 一般都是自定义再使用 跟tableView一样,所有的自定义控件都是要加载在contentView上面
    cell.contentView.backgroundColor = [UIColor redColor];

    return cell;
}

// 10,设置表头表尾,通过代理方法来实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 11,通过判断返回表头还是表尾,通过种类kind来判断
    // 因为参数是字符串的判断相同,不能等号
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 返回表头
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor yellowColor];
        return headerView;
    } else {

    // 返回表尾
      UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter" forIndexPath:indexPath];

        footerView.backgroundColor = [UIColor blueColor];
        return footerView;
    }

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值