您的位置:首页 > 产品设计 > UI/UE

IOS UICollectionView学习

2015-09-22 13:42 417 查看

效果如下:

h文件代码

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic ,strong)UIButton *bgViewButton;
@property(nonatomic ,strong)UIImageView *imgView;
@property(nonatomic ,strong)UILabel *text;

@end


m文件代码

#import "HCollectionViewController.h"

@implementation MyCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [HUtil colorWithHexString:@"dfdfdf"];

self.bgViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0.5, 0, CGRectGetWidth(self.frame)-0.5, CGRectGetWidth(self.frame)-0.5)];
self.bgViewButton.backgroundColor = [UIColor whiteColor];

self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame)/2-16, CGRectGetHeight(self.frame)/2-32, 32, 32)];
self.imgView.backgroundColor = [UIColor clearColor];
[self.bgViewButton addSubview:self.imgView];
//        [self.bgViewButton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.bgViewButton addTarget:self action:@selector(changeBackGroundColor:) forControlEvents:UIControlEventTouchDown];
[self.bgViewButton addTarget:self action:@selector(returnBackGroundColor:) forControlEvents:UIControlEventTouchDragExit];
[self addSubview:self.bgViewButton];

self.text = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(self.imgView.frame)+10, CGRectGetWidth(self.frame)-10, 14)];
self.text.backgroundColor = [UIColor clearColor];
self.text.textAlignment = NSTextAlignmentCenter;
[self.text setFont:[UIFont systemFontOfSize:14]];
[self.text setTextColor:[HUtil colorWithHexString:@"252525"]];
[self addSubview:self.text];

}
return self;
}

- (void)returnBackGroundColor:(id)sender
{
self.bgViewButton.backgroundColor = [CHUtil colorWithHexString:@"ffffff"];
}

- (void)changeBackGroundColor:(id)sender
{
self.bgViewButton.backgroundColor = [CHUtil colorWithHexString:@"efefef"];
}

@end

#import "HCollectionViewController.h"

#define UISCREENHEIGHT  self.view.bounds.size.height
#define UISCREENWIDTH  self.view.bounds.size.width
#define SCROLLVIEWHEIGHT (iPhone6Plus?210:140)

@interface HCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
UICollectionView *mainCollectionView;
}

@end

@implementation CHCollectionViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

[self initCollectionView];

}

- (void)initCollectionView
{
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置collectionView滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionVertical]; //UICollectionViewScrollDirectionVertical  UICollectionViewScrollDirectionHorizontal
//设置headerView的尺寸大小
// layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
//该方法也可以设置itemSize
// layout.itemSize =CGSizeMake(110, 150);

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;

//2.初始化collectionView
mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
[self.view addSubview:mainCollectionView];
mainCollectionView.backgroundColor = [UIColor clearColor];

//3.注册collectionViewCell
//注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
[mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];

//注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
// [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

//4.设置代理
mainCollectionView.delegate = self;
mainCollectionView.dataSource = self;

}

#pragma mark -- UICollectionViewDataSource
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];

cell.text.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
cell.imgView.image = [UIImage imageNamed:@"fabu_"];
//cell.backgroundColor = [UIColor yellowColor];

return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(kDEVICEWIDTH/3.0, kDEVICEWIDTH/3.0);
}

//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}

//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”
/*- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
headerView.backgroundColor =[UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
label.text = @"这是collectionView的头部";
label.font = [UIFont systemFontOfSize:20];
[headerView addSubview:label];
return headerView;
}*/

#pragma mark -- UICollectionViewDelegate
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
NSString *msg = cell.text.text;
NSLog(@"%@",msg);
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: