[TwistedFate]UICollectionView

集合视图

UICollectionViewLayout

抽象类,具体功能由他的子类实现, 网状结构的布局

创建一个网状的结构布局,设置具体的属性
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//  最小行间距
    layout.minimumLineSpacing = 20;

    //  最小列间距
    layout.minimumInteritemSpacing = 20;

    //  设置item大小
    layout.itemSize = CGSizeMake(140, 100);

    //  表头表尾跟随方向(宽与高有一个强制不变)
    //  横向 可设置宽度  竖直 可设置高度
    //  表头的size
    layout.headerReferenceSize = CGSizeMake(100, 200);

    //  表尾size
    layout.footerReferenceSize = CGSizeMake(100, 300);

    //  滚动方向(默认是竖着的)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    //  内边距
    layout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);

    //  设置item强制size
//    layout.estimatedItemSize = CGSizeMake(10, 20);

创建collectionView

//  创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];

    //  设置代理与数据源代理
    collectionView.delegate = self;
    collectionView.dataSource = self;

    //  设置背景颜色(不设置默认黑色)
    collectionView.backgroundColor = [UIColor cyanColor];

    //  显示视图
    [self.view addSubview:collectionView];

    [layout release];
    [collectionView release];
注册要使用的cell,表头表尾
//  注册要使用的cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];

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

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

代理方法实现

遵守协议

//  返回每个分区多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;

}


//  返回每个索引下collectionView的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    //  重用池有可以用的 就拿去用(返回给你)
    //  如果没有 就创建一个
    //  必须注册一下要使用的标识符的cell 才能使用 系统才知道  要从重用池中取出哪个类型的cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

    return cell;

}

//  设置表头表尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    //  判断返回 表头类型还是表尾类型

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //  判断表头

        //  相当于去重用池里找 判断一下 有就用 没有就建
        //  UICollectionElementKindSectionHeader
        //  相当于表头
        //  可重用的View  分为两种 一种表头 一种表尾
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath];

        //  设置颜色
        headerView.backgroundColor = [UIColor greenColor];

        return headerView;

    }else{

        //  表尾
        //  复用的需要注册
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView" forIndexPath:indexPath];

        //  设置颜色
        footView.backgroundColor = [UIColor magentaColor];

        return footView;

    }

}

//  点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值