iOS 自定义UIPickerView地区选择器视图 —— HERO博客

来源:互联网 发布:淘宝打折第二件原价 编辑:程序博客网 时间:2024/05/18 02:09

自定义UIPickerView地区选择器视图 ,首先看一下效果图:

下面贴上相关代码:

ViewController:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end#import "ViewController.h"#import "HWAreaPicker.h"#define mainW [UIScreen mainScreen].bounds.size.width#define mainH [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UITextFieldDelegate, HWAreaPickerDelegate>@property (nonatomic, strong) HWAreaPicker *areaPicker;@property (nonatomic, strong) UITextField *areaTextField;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor = [UIColor blackColor];        //创建控件    [self creatControl];}- (void)creatControl{    //textField    _areaTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)];    _areaTextField.background = [UIImage imageNamed:@"textFieldBj"];    _areaTextField.textAlignment = NSTextAlignmentRight;    _areaTextField.placeholder = @"请设置地区";    _areaTextField.delegate = self;    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.4, mainW * 0.12)];    lab2.textAlignment = NSTextAlignmentLeft;    lab2.text = @"  地区";    lab2.textColor = [UIColor grayColor];    _areaTextField.leftView = lab2;    _areaTextField.leftViewMode = UITextFieldViewModeAlways;    UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15, 0, 15, mainW * 0.12)];    _areaTextField.rightView = lab22;    _areaTextField.rightViewMode = UITextFieldViewModeAlways;    [self.view addSubview:_areaTextField];        //地区选择器    HWAreaPicker *areaPicker = [[HWAreaPicker alloc] initWithFrame:CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)];    areaPicker.delegate = self;    [self.view addSubview:areaPicker];    self.areaPicker = areaPicker;}#pragma mark - UITextFieldDelegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    if (_areaPicker.frame.origin.y != mainH && _areaPicker != nil) {        [_areaPicker dismiss];        return NO;            }else if (textField == _areaTextField) {        [_areaPicker show];        return NO;    }        return YES;}#pragma mark - HWAreaPickerDelegate- (void)didSelectRowInHWAreaPickerView:(HWAreaPicker *)areaPicker{    _areaTextField.text = [NSString stringWithFormat:@"%@ %@ %@", areaPicker.locate.state, areaPicker.locate.city, areaPicker.locate.district];}@end

HWAreaPicker:

#import <UIKit/UIKit.h>@class HWAreaPicker, HWLocationModel;@protocol HWAreaPickerDelegate <NSObject>/** *  HWAreaPicker选中代理事件 * *  @param areaPicker HWAreaPicker */- (void)didSelectRowInHWAreaPickerView:(HWAreaPicker *)areaPicker;@end@interface HWAreaPicker : UIView@property (nonatomic, strong) HWLocationModel *locate;@property (nonatomic, weak) id<HWAreaPickerDelegate> delegate;- (void)show;- (void)dismiss;@end#import "HWAreaPicker.h"//获得屏幕的宽高#define mainW [UIScreen mainScreen].bounds.size.width#define mainH [UIScreen mainScreen].bounds.size.height@interface HWAreaPicker ()<UIPickerViewDelegate, UIPickerViewDataSource>@property (nonatomic, strong) UIPickerView *areaPicker;@property (nonatomic, strong) NSArray *provinces;@property (nonatomic, strong) NSArray *cities;@property (nonatomic, strong) NSArray *areas;@end@implementation HWAreaPicker- (HWLocationModel *)locate{    if (_locate == nil) {        _locate = [[HWLocationModel alloc] init];    }    return _locate;}- (id)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        //背景框        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.9, mainW * 0.5)];        back.image = [UIImage imageNamed:@"areaPickerBj"];        [self addSubview:back];                //地区选择器        _areaPicker = [[UIPickerView alloc] init];        _areaPicker.frame = CGRectMake(10, 10, self.frame.size.width - 20, 120);        _areaPicker.delegate = self;        _areaPicker.dataSource = self;        [self addSubview:_areaPicker];          _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];        _cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];                self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];        self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];                _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];        if (_areas.count > 0) {            self.locate.district = [_areas objectAtIndex:0];        } else{            self.locate.district = @"";        }                //确定按钮        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - mainW * 0.36) * 0.5, self.frame.size.height * 0.747, mainW * 0.36, mainW * 0.11)];        [sureBtn setImage:[UIImage imageNamed:@"sureBtn"] forState:UIControlStateNormal];        [sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:sureBtn];    }    return self;}#pragma mark - UIPickerViewDelegate- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 3;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    switch (component) {        case 0:            return [_provinces count];            break;        case 1:            return [_cities count];            break;        case 2:            return [_areas count];            break;        default:            return 0;            break;    }}- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    switch (component) {        case 0:            return [[_provinces objectAtIndex:row] objectForKey:@"state"];            break;        case 1:            return [[_cities objectAtIndex:row] objectForKey:@"city"];            break;        case 2:            if ([_areas count] > 0) {                return [_areas objectAtIndex:row];                break;            }        default:            return  @"";            break;    }}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    switch (component) {        case 0:            _cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];            [_areaPicker selectRow:0 inComponent:1 animated:YES];            [_areaPicker reloadComponent:1];                        _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];            [_areaPicker selectRow:0 inComponent:2 animated:YES];            [_areaPicker reloadComponent:2];                        self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];            self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];            if ([_areas count] > 0) {                self.locate.district = [_areas objectAtIndex:0];            } else{                self.locate.district = @"";            }            break;        case 1:            _areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];            [_areaPicker selectRow:0 inComponent:2 animated:YES];            [_areaPicker reloadComponent:2];                        self.locate.city = [[_cities objectAtIndex:row] objectForKey:@"city"];            if ([_areas count] > 0) {                self.locate.district = [_areas objectAtIndex:0];            } else{                self.locate.district = @"";            }            break;        case 2:            if ([_areas count] > 0) {                self.locate.district = [_areas objectAtIndex:row];            } else{                self.locate.district = @"";            }            break;        default:            break;    }        if (_delegate && [_delegate respondsToSelector:@selector(didSelectRowInHWAreaPickerView:)]) {        [_delegate didSelectRowInHWAreaPickerView:self];    }}- (void)sureBtnOnClick{    [self dismiss];}- (void)show{    [UIView animateWithDuration:0.3 animations:^{        self.frame = CGRectMake(mainW * 0.05, mainH - mainW * 0.75, mainW * 0.9, mainW * 0.5);    }];}- (void)dismiss{    [UIView animateWithDuration:0.3 animations:^{        self.frame = CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5);    }];}@end

HWLocationModel:

#import <Foundation/Foundation.h>@interface HWLocationModel : NSObject@property (copy, nonatomic) NSString *country;@property (copy, nonatomic) NSString *state;@property (copy, nonatomic) NSString *city;@property (copy, nonatomic) NSString *district;@property (copy, nonatomic) NSString *street;@property (nonatomic) double latitude;@property (nonatomic) double longitude;@end#import "HWLocationModel.h"@implementation HWLocationModel@synthesize country = _country;@synthesize state = _state;@synthesize city = _city;@synthesize district = _district;@synthesize street = _street;@synthesize latitude = _latitude;@synthesize longitude = _longitude;@end





3 0