创建类WJPickerView继承与UIView
ProvinceModel是省市的model,包含属性
@property (nonatomic, strong) NSString *provinceCode;
@property (nonatomic, strong) NSString *provinceName;
@property (nonatomic, strong) NSString *cityCode;
@property (nonatomic, strong) NSString *cityName;
WJPickerView.h
#import <UIKit/UIKit.h>
#import "ProvinceModel.h"
/**
* 回调block
*
* @param currentModel 选中当前省的model
*/
typedef void(^selectProvinceInfoBlock) (ProvinceModel * currentModel);
@interface WJPickerView : UIView
/**
* 返回选择的数据
*
* @param block 回调block
*/
-(void)setSelectProvinceInfoBlock:(selectProvinceInfoBlock)block;
/**
* 显示PickerView
*/
-(void)showPickerView;
@end
WJPickerView.m
#import "WJPickerView.h"
#define PROVINCE_CITYS @"citys"
#define PROVINCE_AND_CITYS_NAME @"name"
#define PROVINCE_AND_CITYS_CODE @"code"
#define PICKER_VIEW_ROW_HEIGHT 30.0f
#define PICKER_VIEW_COMPONENT_COUNT 2 @interface WJPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
/**用来触发pickerView的textField*/
@property(nonatomic,strong)UITextField *inputTextField;
/**pickerView*/
@property(nonatomic,strong)UIPickerView * provincePickerView;
/**存储省市信息*/
@property(nonatomic,strong)NSMutableArray * provinceArray;
/**当前选中第一列的行数*/
@property(nonatomic,assign)NSInteger currentFirstRow;
/**当前选中第二列行数*/
@property(nonatomic,assign)NSInteger currentSecondRow; @property(nonatomic,strong)selectProvinceInfoBlock block; @end
@implementation WJPickerView -(UITextField *)inputTextField{
if (!_inputTextField) {
_inputTextField = [[UITextField alloc] init];
}
return _inputTextField;
} -(UIPickerView *)provincePickerView{
if (!_provincePickerView) {
_provincePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , , )];
_provincePickerView.showsSelectionIndicator = YES;
_provincePickerView.dataSource = self;
_provincePickerView.delegate = self;
}
return _provincePickerView;
} -(instancetype)init{
self = [super init];
if (self) {
/**从plist读取省市信息*/
[self loadAllProvinceData];
/**实例化PickerView并添加到inputView上*/
[self addPickerViewToCurrentView];
}
return self;
} #pragma mark - 从plist读取省市信息
-(void)loadAllProvinceData{
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"province" ofType:@"plist"];
_provinceArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
} #pragma mark - 实例化PickerView并添加到inputView上
-(void)addPickerViewToCurrentView{
[self addSubview:self.inputTextField];
self.inputTextField.inputView = self.provincePickerView;
self.inputTextField.inputView.backgroundColor = [UIColor whiteColor];
/**往inputAccessoryView上添加ToolBar*/
[self addToolBarItem];
} #pragma mark - 往inputAccessoryView上添加ToolBar
-(void)addToolBarItem{
/**textView键盘定制回收按钮*/
UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(, , , )]; UIBarButtonItem * item0 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(hiddenPickerView)]; UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(tapSelectOkButton)]; toolBar.items = @[item0,item1,item2];
self.inputTextField.inputAccessoryView = toolBar; } #pragma mark - 数据回调
-(void)setSelectProvinceInfoBlock:(selectProvinceInfoBlock)block{
if (self.block == nil) {
if (block != nil) {
self.block = block;
}
}
} #pragma mark - 显示pickerView
-(void)showPickerView{
[self.inputTextField becomeFirstResponder];
[self.provincePickerView selectRow:_currentFirstRow inComponent: animated:NO];
[self.provincePickerView selectRow:_currentSecondRow inComponent: animated:NO];
} #pragma mark - 隐藏PickerView
-(void)hiddenPickerView{
[self.superview endEditing:YES];
} #pragma mark - 点击完成
-(void)tapSelectOkButton{ [self hiddenPickerView];
ProvinceModel * provinceModel = [[ProvinceModel alloc] init];
if (_currentFirstRow < _provinceArray.count) {
provinceModel.provinceName = [NSString stringWithFormat:@"%@", _provinceArray[_currentFirstRow][PROVINCE_AND_CITYS_NAME]];
provinceModel.provinceCode = [NSString stringWithFormat:@"%@", _provinceArray[_currentFirstRow][PROVINCE_AND_CITYS_CODE]];
NSArray * citys = _provinceArray[_currentFirstRow][PROVINCE_CITYS];
if (_currentSecondRow < citys.count) {
provinceModel.cityName = [NSString stringWithFormat:@"%@", citys[_currentSecondRow][PROVINCE_AND_CITYS_NAME]];
provinceModel.cityCode = [NSString stringWithFormat:@"%@", citys[_currentSecondRow][PROVINCE_AND_CITYS_CODE]]; if (self.block) {
self.block(provinceModel);
}
}
}
} #pragma mark - UIPickerViewDataSource
/**列数*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return PICKER_VIEW_COMPONENT_COUNT;
} /**行数*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == && _provinceArray != nil) {
return _provinceArray.count;
} if (component == && _provinceArray != nil) {
NSArray * citys = _provinceArray[_currentFirstRow][PROVINCE_CITYS];
return citys.count;
}
return ;
} #pragma mark - UIPickerViewDelegate
/**行高*/
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return PICKER_VIEW_ROW_HEIGHT; } /**自定义PickerView显示的View*/
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *pickerLabel = [[UILabel alloc] initWithFrame:view.frame];
pickerLabel.minimumScaleFactor = ;
pickerLabel.adjustsFontSizeToFitWidth = YES;
pickerLabel.textAlignment = NSTextAlignmentCenter;
pickerLabel.font = [UIFont boldSystemFontOfSize:15.0f];
pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component]; return pickerLabel; } /**每行每列设置title*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == && row < _provinceArray.count) {
return _provinceArray[row][PROVINCE_AND_CITYS_NAME];
}
if (component == ) {
NSArray *citys = _provinceArray[_currentFirstRow][PROVINCE_CITYS];
if (row < citys.count) {
return citys[row][PROVINCE_AND_CITYS_NAME];
} }
return @"";
} /**选中后的回调*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == ) {
_currentFirstRow = row;
}
if (component == ) {
_currentSecondRow = row;
} [self.provincePickerView reloadAllComponents];
}
@end