UICollectionView 添加 HeaderView 和 FooterView 例子

//
//  CaseCluessView.m
//  Created by RockeyCai on 2018/10/22.
//  Copyright © 2018年 . All rights reserved.
//

#import "CaseCluessView.h"
#import "CaseCluessKeybordCollectionViewCell.h"
#import "CaseCluessKeybordCollectionHeadView.h"
#import "CaseCluessKeybordCollectionFootView.h"
#import "CFPopOverView.h"

static NSString *const cellTag = @"CaseCluessKeybordCollectionViewCell";
static NSString *const headTag = @"CaseCluessKeybordCollectionHeadView";
static NSString *const footTag = @"CaseCluessKeybordCollectionFootView";

@interface CaseCluessView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong , nonatomic) BOOL (^checkNetWork)(void);

@property (weak, nonatomic) IBOutlet UICollectionView *keyboardCollectionView;

@property (strong , nonatomic) NSArray *keyboardKeyArray;

//输入框
@property (weak, nonatomic) IBOutlet UITextField *LicenseTF;

@property (weak, nonatomic) IBOutlet UIButton *CityBut;

//下拉菜单
@property (strong , nonatomic) CFPopOverView *CFPopOverView;

@end



@implementation CaseCluessView


+(CaseCluessView *)instance:(BOOL (^)(void))checkNetWork
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"CaseCluessView" owner:nil options:nil];
    CaseCluessView *caseCluessView = [nibView objectAtIndex:0];
    [caseCluessView viewInit];
    caseCluessView.checkNetWork = checkNetWork;
    return caseCluessView;
}

-(void)viewInit{
    [self keyboardCollectionViewSetup];
}


-(void)keyboardCollectionViewSetup{
    
    self.keyboardKeyArray = @[@"0",@"1",@"2",@"3",@"4",@"x",@"5",@"6",@"7",@"8",@"9",@"←",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:cellTag bundle: [NSBundle mainBundle]];
    [self.keyboardCollectionView registerNib:nib forCellWithReuseIdentifier:cellTag];
    self.keyboardCollectionView.dataSource = self;
    self.keyboardCollectionView.delegate = self;
    
    //注册head
    UINib *headNib = [UINib nibWithNibName:headTag bundle: [NSBundle mainBundle]];
    [self.keyboardCollectionView registerNib:headNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headTag];
    
    //注册foot
    UINib *footNib = [UINib nibWithNibName:footTag bundle: [NSBundle mainBundle]];
    [self.keyboardCollectionView registerNib:footNib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footTag];
    

}


/**
 是否隐藏键盘

 @param isHide <#isHide description#>
 */
-(void)hideKeybord:(BOOL)isHide{
    
}


- (IBAction)searchAtion:(id)sender {
}




#pragma mark - UICollectionView 相关

//设置每个区的item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //iPhone
    CGFloat width = (self.frame.size.width - 8) / 6;
    //返回每个item的大小
    return CGSizeMake( width , width );
    
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(4, 4, 4, 4);
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


/**
 个数
 
 @param collectionView <#collectionView description#>
 @param section <#section description#>
 @return <#return value description#>
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  self.keyboardKeyArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CaseCluessKeybordCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellTag forIndexPath:indexPath];
    NSString *key = [self.keyboardKeyArray objectAtIndex:indexPath.row];
    [cell viewInit:key];
    
    
    return cell;
}


/**
 选中
 
 @param collectionView <#collectionView description#>
 @param indexPath <#indexPath description#>
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSString *key = [self.keyboardKeyArray objectAtIndex:indexPath.row];
    NSLog(@"---%@---" , key);
    
    //全部清除
    if([key isEqualToString:@"x"]){
        self.LicenseTF.text = @"";
        return;
    }else if ([key isEqualToString:@"←"]){
        //删除单个字符
        NSString *temp = self.LicenseTF.text;
        if (temp.length > 0) {
            temp = [temp substringToIndex:temp.length - 1];
            self.LicenseTF.text = temp;
        }
        return;
    }
    
    self.LicenseTF.text = [NSString stringWithFormat:@"%@%@" , self.LicenseTF.text , key];
    
}

//header的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if(self.keyboardKeyArray.count == 0){
        
        return CGSizeZero;
        
    }else{
        
        CGFloat height = (self.frame.size.width - 8) / 6;
        return CGSizeMake(self.frame.size.width, height - 8);
    }
    
}

//footer的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    CGFloat height = (self.frame.size.width - 8) / 6;
    return CGSizeMake(self.frame.size.width, height - 8);
}

//header 和 footer的获取逻辑
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    __weak typeof(self) weakSelf = self;
    
    UICollectionReusableView *view = nil;
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        CaseCluessKeybordCollectionHeadView *temp = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headTag forIndexPath:indexPath];
        temp.cityAndNumBlock = ^(NSString *city, NSString *num) {
        };
        view = temp;
    }else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
        CaseCluessKeybordCollectionFootView *temp = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footTag forIndexPath:indexPath];
        temp.hideKeybordBlock = ^(BOOL hideFlag) {
            
        };
        view = temp;
    }
   
    return view;
}


@end

 

 

header或者footer 都必须继承 UICollectionReusableView

#import <UIKit/UIKit.h>

@interface CaseCluessKeybordCollectionHeadView : UICollectionReusableView

//城市和编码 例如粤A , 粤B , 粤C
@property (copy , nonatomic)void (^cityAndNumBlock)(NSString *,NSString *);

@end



#import "CaseCluessKeybordCollectionHeadView.h"
#import "AppConfig4OC.h"

@interface CaseCluessKeybordCollectionHeadView()

@property (weak, nonatomic) IBOutlet UIButton *but1;

@property (weak, nonatomic) IBOutlet UIButton *but2;

@property (weak, nonatomic) IBOutlet UIButton *but3;
@end


@implementation CaseCluessKeybordCollectionHeadView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.but1.layer.cornerRadius = 4.0;//2.0是圆角的弧度,根据需求自己更改
    self.but1.layer.borderColor = UIColorFromRGB(0Xeff1f1).CGColor;//设置边框颜色
    self.but1.layer.borderWidth = 1.0f;//设置边框颜色
    
    
    self.but2.layer.cornerRadius = 4.0;//2.0是圆角的弧度,根据需求自己更改
    self.but2.layer.borderColor = UIColorFromRGB(0Xeff1f1).CGColor;//设置边框颜色
    self.but2.layer.borderWidth = 1.0f;//设置边框颜色
    
    
    self.but3.layer.cornerRadius = 4.0;//2.0是圆角的弧度,根据需求自己更改
    self.but3.layer.borderColor = UIColorFromRGB(0Xeff1f1).CGColor;//设置边框颜色
    self.but3.layer.borderWidth = 1.0f;//设置边框颜色
}


- (IBAction)butAction:(UIButton *)sender {
    
    if(self.cityAndNumBlock){
        
        //self.cityAndNumBlock(city , num);
    }
    
}


@end

 

Cell

@interface CaseCluessKeybordCollectionViewCell : UICollectionViewCell


-(void)viewInit:(NSString *)key;

@end



#import "CaseCluessKeybordCollectionViewCell.h"
#import "AppConfig4OC.h"

@interface CaseCluessKeybordCollectionViewCell()

@property (weak, nonatomic) IBOutlet UIButton *but;


@end


@implementation CaseCluessKeybordCollectionViewCell

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.but.layer.cornerRadius = 4.0;//2.0是圆角的弧度,根据需求自己更改
    self.but.layer.borderColor = UIColorFromRGB(0Xeff1f1).CGColor;//设置边框颜色
    self.but.layer.borderWidth = 1.0f;//设置边框颜色
}

-(void)viewInit:(NSString *)key{
    
    [self.but setTitle:key forState:UIControlStateNormal];
}



@end

Cell还是header footer都是通过nib注册的,所以都各种要新建一个nib文件,并制定class是相应的类型。

 

完结。。。

转载于:https://my.oschina.net/huqiji/blog/2251106

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值