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