iOS编程------集合视图UICollectionView

本文详细介绍了如何在iOS应用中使用UICollectionView实现复杂的布局样式,并通过数据绑定展示分区和元素数量。包括布局配置、单元格重用、区头区尾的加载以及点击事件的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  AppDelegate.h
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end






//
//  AppDelegate.m
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}








///////////////////////////////////////////////////////////////////






//
//  ViewController.h
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end






//
//  ViewController.m
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "MyCollectionViewCell2.h"

#define KCell @"mycell"
#define KHeader @"header"
#define KFooter @"footer"


@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> //创建collectionView遵守的两个协议,类似UITableView.

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    /*
    创建布局样式
    UICollectionViewLayout, 为一个抽象类,我们通常使用它的子类,系统提供一个子类.
    UICollectionViewLayout, 称为网格布局,对齐到网格.
    */

    //(1)创建布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    //(2)配置布局对象
    flowLayout.minimumLineSpacing = 10;                                    //行间距
    flowLayout.minimumInteritemSpacing = 10;                               //列间距

    CGFloat width = (self.view.bounds.size.width - 10 * 2 - 10 * 2) / 3;
    CGFloat height = 2 * width;
    flowLayout.itemSize = CGSizeMake(width, height);                        //单元格宽高

    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);             //分区边距
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;   //滚动方向

    /*
    注意:区头大小和区尾的宽高只有一个起作用,由混动方向决定.
    如果是竖直滚动,宽度为集合视图宽度 collectionView.size.width,高度可以指定
    如果是水平滚动,高度为集合视图高度,宽度可以指定.
    */
    flowLayout.headerReferenceSize = CGSizeMake(0, 100); //区头大小
    flowLayout.footerReferenceSize = CGSizeMake(0, 50);  //区尾大小


    /*
    创建一个集合视图对象
    集合视图不仅需要指定frame,还需要指定布局样式.
    */

    //(1)创建
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];

    //(2)设置delegate dataSource 等等.
    collectionView.delegate = self;
    collectionView.dataSource = self;

    collectionView.backgroundColor = [UIColor redColor];

    //(3)注册单元格,(区头, 区尾) 两种方式, 纯代码和xib

    //单元格
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:KCell];



    //xib方式
//    [collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell2" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];




    //区头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeader];

    //区尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:KFooter];

    //(4) 添加到父视图上
    [self.view addSubview:collectionView];


}


#pragma mark---------- 实现协议里面的方法 -------------

//分区中的元素数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 30;
}

//给元素加载单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    //1.从重用队列里面取
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCell forIndexPath:indexPath];

    //2.配置单元格
    //设置图片
    cell.imageView.image = [UIImage imageNamed:@"image2.jpg"];
    //设置label
    cell.label.text = [NSString stringWithFormat:@"Sec:%ld Item:%ld", indexPath.section, indexPath.item];


    //xib方式
//    MyCollectionViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


    //3.返回
    return cell;
}

//加载区头,区尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    //根据kind去判断,加载的是区头还是区尾
    if (kind == UICollectionElementKindSectionHeader) {

        //(1)取出
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeader forIndexPath:indexPath];
        //(2)配置
        headerView.backgroundColor = [UIColor yellowColor];
        //(3)返回
        return headerView;

    } else {

        //(1)取出
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:KFooter forIndexPath:indexPath];
        //(2)配置
        footerView.backgroundColor = [UIColor greenColor];
        //(3)返回
        return footerView;

    }
}


//当单元格被选中的时候触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"分区号:%ld 元素下表:%ld", indexPath.section, indexPath.item);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end







////////////////////////////////////////////////////////////////////






//
//  MyCollectionViewCell.h
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property (nonatomic, strong, readonly) UIImageView *imageView; //显示图片
@property (nonatomic, strong, readonly) UILabel *label;         //显示的label,展示分区号,item号


@end






//
//  MyCollectionViewCell.m
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {

        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_imageView];

        _label = [[UILabel alloc] initWithFrame:(CGRectMake(0, self.bounds.origin.y , self.bounds.size.width, 40))];
        [self.contentView addSubview:_label];

    }
    return self;
}


@end





///////////////////////////////////////////////////////////////////






//
//  MyCollectionViewCell2.h
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell2 : UICollectionViewCell


@property (strong, nonatomic) IBOutlet UIImageView *imageView;


@end






//
//  MyCollectionViewCell2.m
//  UI21_UICollectionView
//
//  Created by l on 15/10/5.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "MyCollectionViewCell2.h"

@implementation MyCollectionViewCell2

- (void)awakeFromNib {
    // Initialization code
}



@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值