iOS大典之集合视图


项目遇到了集合视图,
感觉得整理下

又来一篇


遵守协议

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

// 创建一个集合视图
- (void)addSubViews
{

    // Item 布局 (网格状布局)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    // 行边距
    layout.minimumLineSpacing = 30;
    // 列边距
    layout.minimumInteritemSpacing = 30;

    // 设置item的宽高
    layout.itemSize = CGSizeMake(350, 450);

    // 设置滑动方向(默认是上下滑动)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // 设置表头 只有高度影响表头
    layout.headerReferenceSize = CGSizeMake(0, 200);
    // 设置表尾 只有高度影响表尾
    layout.footerReferenceSize = CGSizeMake(0, 100);

    // 设置内边距
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);

    // 初始化 集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; // 需要一个布局,先初始化一个

    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;

    // collectionView 默认是黑色
    collectionView.backgroundColor = [UIColor cyanColor];

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

    // 注册要用的cell
    // Identifier 重用标示符 要一致
    // <#(Class)#> 你的cell是那个类的 就添加那个类
    // 使用系统的就注册系统的 如果自定义的话 就注册自定义的
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];



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

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


}



#pragma mark -- dataSource --


// 返回分区数, 默认就1个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回每个Item的方法
    // 必须有一步: 注册cell

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];


    // 使用UICollectionViewCell 一般都是自定义再使用 和tableView 一样 所有的自定义控件 都要加在contentView 上面
    cell.contentView.backgroundColor = [UIColor grayColor];

    return cell; // 不用释放

}


// 设置表头表尾 同代理方法 来实现

- (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;

    }
}



over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值