2022-07-19GYSingleSelectPicker

//  单列选择器

#import

typedefvoid(^ GYSingleSelectPickerBlock) (NSIntegerindex);

@interface GYSingleSelectPicker : UIView<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableArray      *arr;

@property (nonatomic, assign) NSInteger            indexSelect;

@property (nonatomic,copy) GYSingleSelectPickerBlock singleSelectPickerBlock;

@property (nonatomic, strong) UIButton      *bgButton;

/*  初始化方法

    frame :选择框的frame

    arrData : 要展示的数据

*/

- (instancetype)initWithFrame:(CGRect)framearr:(NSArray*)arrDataisAuto:(BOOL)isAuto;

@end


#import "GYSingleSelectPicker.h"

#define PickerHeight240

@implementation GYSingleSelectPicker

- (instancetype)initWithFrame:(CGRect)framearr:(NSArray*)arrDataisAuto:(BOOL)isAuto

{

    self= [superinitWithFrame:frame];

    if(self)

    {

        [self setBackgroundColor:[UIColor whiteColor]];

        self.arr = [[NSMutableArray alloc ] initWithArray:arrData];

        if(isAuto)

        {

            self.frame=CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);

            [selfinitCtrl];

        }

        else{

            [selfinitCtrl:frame];

        }

    }

    return self;

}

-(void)initCtrl

{

    self.backgroundColor = [UIColor clearColor];

    UIButton *bgButton = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [bgButtonaddTarget:self action:@selector(closeButton) forControlEvents:UIControlEventTouchUpInside];

    bgButton.backgroundColor=RGBA(0,0,0,0.2);

    [selfaddSubview:bgButton];


    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-PickerHeight, SCREEN_WIDTH, PickerHeight)];

    bgView.backgroundColor = [UIColor whiteColor];

    [selfaddSubview:bgView];


    UIButton *btnCancel = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 80, 40)];

    [btnCancelsetTitle:@"取消" forState:UIControlStateNormal];

    [btnCancelsetTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];

    [btnCanceladdTarget:self action:@selector(onButtonCancel) forControlEvents:UIControlEventTouchUpInside];

    [bgViewaddSubview:btnCancel];


    UILabel *selectLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, SCREEN_WIDTH-160, 40)];

    selectLabel.text=@"请选择";

    selectLabel.font = [UIFont systemFontOfSize:15];

    selectLabel.textColor=UIColorFromRGB(0x333333);

    selectLabel.textAlignment = NSTextAlignmentCenter;

    [bgViewaddSubview:selectLabel];


    UIButton *btnFinish = [[UIButton alloc ] initWithFrame:CGRectMake(self.frame.size.width-80, 0, 80, 40)];

    [btnFinishsetTitle:@"完成" forState:UIControlStateNormal];

    [btnFinishsetTitleColor:UIColorFromRGB(UI_COLOR_Exam_Red) forState:UIControlStateNormal];

    [btnFinishaddTarget:self action:@selector(onButtonFinish) forControlEvents:UIControlEventTouchUpInside];

    [bgViewaddSubview:btnFinish];


    UIPickerView *pickerView = [[UIPickerView alloc ] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH,PickerHeight-40)];

    pickerView.delegate=self;

    pickerView.dataSource=self;

    [pickerViewsetBackgroundColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];

    [bgViewaddSubview:pickerView];

}

- (void)closeButton{

    [self onButtonCancel];

}

-(void)initCtrl:(CGRect)frame

{

    UIButton *btnCancel = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 80, 40)];

    [btnCancelsetTitle:@"取消" forState:UIControlStateNormal];

    [btnCancelsetTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];

    [btnCanceladdTarget:self action:@selector(onButtonCancel) forControlEvents:UIControlEventTouchUpInside];

    [selfaddSubview:btnCancel];


    UILabel *selectLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, SCREEN_WIDTH-160, 40)];

    selectLabel.text=@"请选择";

    selectLabel.font = [UIFont systemFontOfSize:15];

    selectLabel.textColor=UIColorFromRGB(0x333333);

    selectLabel.textAlignment = NSTextAlignmentCenter;

    [selfaddSubview:selectLabel];


    UIButton *btnFinish = [[UIButton alloc ] initWithFrame:CGRectMake(self.frame.size.width-80, 0, 80, 40)];

    [btnFinishsetTitle:@"完成" forState:UIControlStateNormal];

    [btnFinishsetTitleColor:UIColorFromRGB(UI_COLOR_Exam_Red) forState:UIControlStateNormal];

    [btnFinishaddTarget:self action:@selector(onButtonFinish) forControlEvents:UIControlEventTouchUpInside];

    [selfaddSubview:btnFinish];


    UIPickerView *pickerView = [[UIPickerView alloc ] initWithFrame:CGRectMake(0, 40, frame.size.width, frame.size.height-40)];

    pickerView.delegate=self;

    pickerView.dataSource=self;

    [pickerViewsetBackgroundColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];

    [selfaddSubview:pickerView];

}

-(void) onButtonCancel

{

    if(self)

    {

        [self removeFromSuperview];

    }

}

-(void) onButtonFinish

{

    if (self.singleSelectPickerBlock) {

        self.singleSelectPickerBlock(_indexSelect);

    }

    //点击完成后,关闭当前view

    [self onButtonCancel];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

    return1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    return self.arr.count;

}

- (NSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)rowforComponent:(NSInteger)component

{

    return[self.arrobjectAtIndex:row];

}

- (void)pickerView:(UIPickerView*)pickerViewdidSelectRow:(NSInteger)rowinComponent:(NSInteger)component

{

//    NSLog(@"%@",self.arr[row]);

    _indexSelect = row;

}

- (CGFloat)pickerView:(UIPickerView*)pickerViewrowHeightForComponent:(NSInteger)component{

    return35;

}

@end



#import "GYAreaPicker.h"

#import "JobAddressModel.h"

typedefvoid(^ GYAreaPickerBlock) (NSString*addressStr);

@interface GYAreaPicker : UIView<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableArray      *arr;

@property (nonatomic, strong) NSMutableArray      *subObjects;

@property (nonatomic, assign) NSInteger            rightIndexSelect;

@property (nonatomic, assign) NSInteger            leftIndexSelect;

@property (nonatomic, copy) NSString *addressStr;

@property (nonatomic, strong) GYAreaPickerBlock areaPickerBlock;

@property (nonatomic, strong) UIButton      *bgButton;

- (instancetype)initWithFrame:(CGRect)framearr:(NSArray*)arrData;

@end

#import "GYAreaPicker.h"

#define PickerHeight240

@implementation GYAreaPicker

- (instancetype)initWithFrame:(CGRect)framearr:(NSArray*)arrData

{

    self= [superinitWithFrame:frame];

    if(self)

    {

        [self setBackgroundColor:[UIColor whiteColor]];

        self.arr = [[NSMutableArray alloc ] init];

        self.subObjects = [[NSMutableArray alloc ] init];

        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

        self.leftIndexSelect = 0;

        for(NSDictionary*dicinarrData) {

            JobAddressModel *model = [JobAddressModel mj_objectWithKeyValues:dic];

            [self.arraddObject:model];

        }

        [self.arr removeObjectAtIndex:0];//删掉全部这个项

        JobAddressModel*model =self.arr[0];

        NSArray*arr = model.sub;

        //如果有数据则展示二级城市

        if(arr.count==0) {

            JobAddressModel *model1 = [[JobAddressModel alloc]init];

            model1 = model;

            model1.showName=@"全部";

            model1.fatherId= model.id;

            [self.subObjectsaddObject:model1];

        }

        for(NSDictionary*dicinarr) {

            JobAddressModel *model2 = [JobAddressModel mj_objectWithKeyValues:dic];

            model2.showName= model2.name;

            model2.fatherId= model.id;

            [self.subObjectsaddObject:model2];

        }

        [selfinitCtrl];

    }

    return self;

}

-(void)initCtrl

{

    self.backgroundColor = [UIColor clearColor];

    UIButton *bgButton = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [bgButtonaddTarget:self action:@selector(closeButton) forControlEvents:UIControlEventTouchUpInside];

    bgButton.backgroundColor=RGBA(0,0,0,0.2);

    [selfaddSubview:bgButton];

    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-PickerHeight, SCREEN_WIDTH, PickerHeight)];

    bgView.backgroundColor = [UIColor whiteColor];

    [selfaddSubview:bgView];

    UIButton *btnCancel = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 80, 40)];

    [btnCancelsetTitle:@"取消" forState:UIControlStateNormal];

    [btnCancelsetTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];

    [btnCanceladdTarget:self action:@selector(onButtonCancel) forControlEvents:UIControlEventTouchUpInside];

    [bgViewaddSubview:btnCancel];

    UILabel *selectLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, SCREEN_WIDTH-160, 40)];

    selectLabel.text=@"请选择";

    selectLabel.font = [UIFont systemFontOfSize:15];

    selectLabel.textColor=UIColorFromRGB(0x333333);

    selectLabel.textAlignment = NSTextAlignmentCenter;

    [bgViewaddSubview:selectLabel];

    UIButton *btnFinish = [[UIButton alloc ] initWithFrame:CGRectMake(self.frame.size.width-80, 0, 80, 40)];

    [btnFinishsetTitle:@"完成" forState:UIControlStateNormal];

    [btnFinishsetTitleColor:UIColorFromRGB(UI_COLOR_Exam_Red) forState:UIControlStateNormal];

    [btnFinishaddTarget:self action:@selector(onButtonFinish) forControlEvents:UIControlEventTouchUpInside];

    [bgViewaddSubview:btnFinish];

    UIPickerView *pickerView = [[UIPickerView alloc ] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH,PickerHeight-40)];

    pickerView.delegate=self;

    pickerView.dataSource=self;

    [pickerViewsetBackgroundColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];

    [bgViewaddSubview:pickerView];

}

- (void)closeButton{

    [self onButtonCancel];

}

-(void) onButtonCancel

{

    if(self)

    {

        [self removeFromSuperview];

    }

}

-(void) onButtonFinish

{

    JobAddressModel *model = self.arr[self.leftIndexSelect];

    JobAddressModel *model1 = self.subObjects[self.rightIndexSelect];

    _addressStr= [NSStringstringWithFormat:@"%@%@",model.name,model1.name];

    if (self.areaPickerBlock) {

        self.areaPickerBlock(_addressStr);

    }

    //点击完成后,关闭当前view

    [self onButtonCancel];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

    return2;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    if(component ==0) {

        returnself.arr.count;

    }

    elseif(component ==1){

        returnself.subObjects.count;

    }

    return0;

}

- (NSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)rowforComponent:(NSInteger)component

{

    if(component ==0) {

        JobAddressModel*model =self.arr[row];

        returnmodel.name;

    }

    elseif(component ==1){

        JobAddressModel*model =self.subObjects[row];

        returnmodel.name;

    }

    else{

        return@"";

    }

}

- (void)pickerView:(UIPickerView*)pickerViewdidSelectRow:(NSInteger)rowinComponent:(NSInteger)component

{

    if(component ==0) {

        self.leftIndexSelect= row;

        self.rightIndexSelect = 0;

        JobAddressModel *model = self.arr[self.leftIndexSelect];

        NSArray*arr = model.sub;

        //如果有数据则展示二级城市

        if(arr.count==0) {

            JobAddressModel *model1 = [[JobAddressModel alloc]init];

            model1 = model;

            model1.showName=@"全部";

            model1.fatherId= model.id;

            [self.subObjectsaddObject:model1];

        }

        [self.subObjects removeAllObjects];

        for(NSDictionary*dicinarr) {

            JobAddressModel *model2 = [JobAddressModel mj_objectWithKeyValues:dic];

            model2.showName= model2.name;

            model2.fatherId= model.id;

            [self.subObjectsaddObject:model2];

        }

        [pickerViewreloadComponent:1];

    }

    elseif(component ==1){

        self.rightIndexSelect= row;

    }

}

- (CGFloat)pickerView:(UIPickerView*)pickerViewrowHeightForComponent:(NSInteger)component{

    return35;

}

@end


@interface JobAddressModel : NSObject

@property(nonatomic, copy) NSString *firstLetter;//城市首字母

@property(nonatomic, copy) NSString *showName;//展示的城市名字  --非接口给的数据

@property(nonatomic, assign) int id;//城市id

@property(nonatomic, copy) NSString *name;//城市名称

@property(nonatomic, copy) NSString *parentId;

@property (nonatomic, strong) NSArray *sub;//子城市

@property(nonatomic, copy) NSString *code;

@property(nonatomic, copy) NSString *fullName;

@property(nonatomic, copy) NSString *level;//层级 0 第一层 1第二层

@property(nonatomic, copy) NSString *weight;

@property(nonatomic, assign) int fatherId;//如果是子城市,这里存一下父亲的id --非接口给的数据

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容