版权声明:本文为延成原创文章,转载请标明出处
VCUICollectionView
#import "VCUICollectionView.h"
#import "CollectionViewCell.h"
@interface VCUICollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
NSArray* _imageArray;
UICollectionViewFlowLayout* _flowLayout;
}
@property(nonatomic,strong)UIImageView* headerImage;
@end
@implementation VCUICollectionView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"UICollectionView";
_imageArray = @[@"guide_1",@"guide_2",@"guide_3",@"guide_4",@"guide_5",@"guide_6",@"guide_7",@"guide_8",@"guide_9"];
[self initView];
}
-(void)initView{
UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
UICollectionView* collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) collectionViewLayout:layout];
collectionView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[CollectionViewCell class]forCellWithReuseIdentifier:@"cellid"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" ];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier: @"CollectionFooterView"];
collectionView.showsVerticalScrollIndicator = NO;
[self.view addSubview:collectionView];
}
//分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
//每个分区item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
cell.itemImageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
cell.titleLabel.text = _imageArray[indexPath.row];
return cell;
}
//点击cell响应事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
}
#pragma mark - collectionViewLayout
//cell大小 cell的宽度决定了一行有几个cell
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width/2 - 20, 100);
}
//行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
//设置cell的外边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 0, 10);
}
//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.view.bounds.size.width, 250);
}
//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(self.view.bounds.size.width, 80);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView* header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
[self addContent];
[header addSubview:self.headerImage];
return header;
} else {
UICollectionReusableView* footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFooterView" forIndexPath:indexPath];
return footer;
}
}
-(void)addContent
{
UIImageView *headerImage=[[UIImageView alloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);
headerImage.image=[UIImage imageNamed:@"guide_5"];
self.headerImage=headerImage;
}
#pragma mark - collectionViewCell点击高亮
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor lightGrayColor];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
@end
CollectionViewCell.m
#import "CollectionViewCell.h"
@implementation CollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 6;
_titleLabel = [[UILabel alloc] init];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.titleLabel];
_itemImageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.itemImageView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.itemImageView.bounds = CGRectMake(0, 0, 50, 50);
self.itemImageView.center = CGPointMake(self.bounds.size.width/2, 30);
self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.itemImageView.frame) + 5, self.bounds.size.width, 30);
}
@end