iOS UICollectionView的详细介绍

首先要遵循三个协议

// 必须遵守的三个协议
@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

- 

- (void)viewDidLoad {
    [super viewDidLoad];
   
    UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每一个 cell 的大小 就是宽和高
    [FlowLayout setItemSize:CGSizeMake(100, 100)];
    // UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) 其中top和bottom控制的是距离顶部和底部的高度,并不能控制各个cellectionCell之间的距离,left和right能够控制各个cellectionCell之间的距离
    FlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);// 设置每一个 cell 之间的边界
    // 创建
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:FlowLayout];
    collectionView.backgroundColor = [UIColor redColor];
    //设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    // 注册头部
    
    // 注册 cell 否则无法显示
    [ collectionView registerClass:[TestCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    
    [self.view addSubview:collectionView];
}

#pragma mark -- UICollectionViewCellde de 代理方法
// 控制有几个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
// 每个分区显示的 cell 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
// 返回 cell 的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idnetifier = @"collectionViewCell";
    TestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idnetifier forIndexPath:indexPath];
    
    
    return cell;
}
#pragma marl - UICollectionViewCell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击的是第%ld个分区,的第%ld 行", (long)indexPath.section,(long)indexPath.row);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

第二部
创建 cell 继承自 UIcollectionViewCell

#import "TestCollectionViewCell.h"

@implementation TestCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pictureImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
        self.pictureImage.image = [UIImage imageNamed:@"1.jpg"];
        [self.contentView addSubview:self.pictureImage];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 80, 30)];
        self.titleLabel.text = @"标题";
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值