标签:
基本属性
1.UIPickerView的常见属性
// 数据源(用来告诉UIPickerView有多少列多少行)
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
@property(nonatomic,assign) id<UIPickerViewDelegate> delegate;
// 是否要显示选中的指示器
@property(nonatomic) BOOL showsSelectionIndicator;
// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents;
2.UIPickerView的常见方法
// 重新刷新所有列
- (void)reloadAllComponents;
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component;
// 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
// 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;
3.数据源方法(UIPickerViewDataSource)
// 一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
4.代理方法(UIPickerViewDelegate)
// 第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
// 第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 第component列第row行显示怎样的view(内容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
// 选中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
代码示例:
mePickView.h
//
// mePickView.h
// cityPickerDemo
//
// Created by 王龙 on 16/3/24.
// Copyright © 2016年 Larry(Lawrence). All rights reserved.
//
#import <UIKit/UIKit.h>
@class mePickView;
@protocol mePickViewDelegate <NSObject>
-(void)selectCity:(NSString *)city;
@end
@interface mePickView : UIView
@property(nonatomic, assign) id<mePickViewDelegate>delegate;
@end
mePickView.m
//
// mePickView.m
// cityPickerDemo
//
// Created by 王龙 on 16/3/24.
// Copyright © 2016年 Larry(Lawrence). All rights reserved.
//
#import "mePickView.h"
@interface mePickView ()<UIPickerViewDataSource,UIPickerViewDelegate>
//数据
@property (nonatomic,strong) NSDictionary *pickerDic;
@property (nonatomic,strong) NSArray *provinceArray;
@property (nonatomic,strong) NSArray *cityArray;
@property (nonatomic,strong) NSArray *townArray;
@property (nonatomic,strong) NSArray *selectedArray;
//视图
@property (nonatomic,strong) UIPickerView *pickerView;
@end
@implementation mePickView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self getPickerData];
[self addView];
}
return self;
}
//初始化视图
-(void)addView{
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-35)];
_pickerView.delegate = self;
_pickerView.dataSource = self;
[self addSubview:_pickerView];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, CGRectGetMaxY(_pickerView.frame), self.bounds.size.width, 35);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button addTarget:self action:@selector(selectjia) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
//选择城市
-(void)selectjia{
NSLog(@"城市 == %@%@%@",[self.provinceArray objectAtIndex:[self.pickerView selectedRowInComponent:0]],[self.cityArray objectAtIndex:[self.pickerView selectedRowInComponent:1]],[self.townArray objectAtIndex:[self.pickerView selectedRowInComponent:2]]);
NSString * city = [NSString stringWithFormat:@"%@%@%@",[self.provinceArray objectAtIndex:[self.pickerView selectedRowInComponent:0]],[self.cityArray objectAtIndex:[self.pickerView selectedRowInComponent:1]],[self.townArray objectAtIndex:[self.pickerView selectedRowInComponent:2]]];
[self.delegate selectCity:city];
}
- (void)getPickerData{
NSString *path = [[NSBundle mainBundle]pathForResource:@"Address" ofType:@"plist"];
self.pickerDic = [[NSDictionary alloc]initWithContentsOfFile:path];
self.provinceArray = [self.pickerDic allKeys];
self.selectedArray = [self.pickerDic objectForKey:[[self.pickerDic allKeys] objectAtIndex:0]];
if (self.selectedArray.count > 0) {
self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
}
if (self.cityArray.count > 0) {
self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
}
}
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
//每列的个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return self.provinceArray.count;
}else if (component == 1){
return self.cityArray.count;
}else{
return self.townArray.count;
}
}
//返回当前行的内容 此处是将数组中的值添加上去
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return [self.provinceArray objectAtIndex:row];
}else if (component == 1){
return [self.cityArray objectAtIndex:row];
}else{
return [self.townArray objectAtIndex:row];
}
}
//每列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component == 0) {
return 110;
}else if (component == 1){
return 100;
}else{
return 110;
}
}
//返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:row]];
if (self.selectedArray.count > 0) {
self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
} else {
self.cityArray = nil;
}
if (self.cityArray.count > 0) {
self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
} else {
self.townArray = nil;
}
}
[pickerView selectedRowInComponent:1];
[pickerView reloadComponent:1];
[pickerView selectedRowInComponent:2];
if (component == 1) {
if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:row]];
} else {
self.townArray = nil;
}
[pickerView selectRow:1 inComponent:2 animated:YES];
}
[pickerView reloadComponent:2];
}
@end
ViewController.h 不写东西
ViewController.m
//
// ViewController.m
// cityPickerDemo
//
// Created by 王龙 on 16/3/24.
// Copyright © 2016年 Larry(Lawrence). All rights reserved.
//
#import "ViewController.h"
#import "mePickView.h"
@interface ViewController ()<mePickViewDelegate>
@property(nonatomic, weak)UILabel * label;
@property(nonatomic, strong)mePickView * pickView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(00,120, self.view.bounds.size.width, 45)];
label.backgroundColor = [UIColor orangeColor];
label.textAlignment = NSTextAlignmentCenter;
self.label = label;
[self. view addSubview:label];
_pickView = [[mePickView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame)+10, self.view.bounds.size.width, 180)];
_pickView.backgroundColor = [UIColor greenColor];
_pickView.delegate = self;
[self.view addSubview:_pickView];
}
-(void)selectCity:(NSString *)city{
self.label.text = city;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
标签:
原文地址:http://www.cnblogs.com/ios-wanglong/p/5314956.html