UICollectionView 简单使用详解

UICollectionView 其实和UITableview差不多,学会简单的使用后,根据 UITableview 的各种使用逻辑,就能够轻松的实现你想要实现的各种功能,下面直接上代码啦


1,ViewController.h 文件

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//    flowLayout 的滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//    两个 item 之间 的 横向间隔
    flowLayout.minimumInteritemSpacing = 20;
//    两个 item 之间 的 纵向间隔
    flowLayout.minimumLineSpacing = 10;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
//    初始化 UICollectionView 的时候,同时 注册 cell
    [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellID];
    
}

// 定义每组 Items 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
    cell.imageVieww.image = [UIImage imageNamed:@"小猴猴"];
    cell.nameLabel.text = @"你哈";
    return cell;
    
}

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    cell 的宽高
    return CGSizeMake(80, 80);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(50, 15, 50, 15);//(上边距,左边距,下边距,右边距)
}

#pragma mark ------ 选中 cell -------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"点击的是%@",cell.nameLabel.text);
        cell.backgroundColor = [UIColor yellowColor];
}

#pragma mark ----取消选中上一个  -------
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
        CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor cyanColor];
}



2, 自定义的 

UICollectionViewCell


.h 文件 

#import <UIKit/UIKit.h>

@interface CollectionViewCell : UICollectionViewCell

@property (nonatomic, retain)UIImageView *imageVieww;

@property (nonatomic, retain)UILabel *nameLabel;

@end

.m 文件

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "CollectionViewCell.h"

@implementation CollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageVieww = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/5, WIDTH/5)];
        self.imageVieww.layer.cornerRadius = WIDTH/5/2;
        self.imageVieww.layer.masksToBounds = YES;
        self.imageVieww.layer.borderWidth = 2;
        self.imageVieww.layer.borderColor = [UIColor cyanColor].CGColor;
        [self.contentView addSubview:self.imageVieww];
        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageVieww.frame)-20, CGRectGetWidth(self.imageVieww.frame), 20)];
        self.nameLabel.textColor = [UIColor yellowColor];
        self.nameLabel.textAlignment = 1;
        [self.contentView addSubview:self.nameLabel];
    }
    return self;
}

效果图如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值