// CaseCluessView.m // Created by RockeyCai on 2018/10/22. #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() @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; @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 { //设置每个区的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;

header或者footer 都必须继承 UICollectionReusableView

#import <UIKit/UIKit.h>
@interface CaseCluessKeybordCollectionHeadView : UICollectionReusableView
//城市和编码 例如粤A , 粤B , 粤C
@property (copy , nonatomic)void (^cityAndNumBlock)(NSString *,NSString *);
#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;
@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);
@interface CaseCluessKeybordCollectionViewCell : UICollectionViewCell
-(void)viewInit:(NSString *)key;
#import "CaseCluessKeybordCollectionViewCell.h"
#import "AppConfig4OC.h"
@interface CaseCluessKeybordCollectionViewCell()
@property (weak, nonatomic) IBOutlet UIButton *but;
@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];

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

完结。。。

展开阅读全文