使用UIPickerView可以在用户摄者时提供便利,尤其是固定的信息中:如省市的选择,性别的选择
数据源的获取:
#import <Foundation/Foundation.h>
@interface Province : NSObject
@property (copy, nonatomic)NSString *name;
@property (strong, nonatomic)NSArray *cities;
- (instancetype)initWithDic:(NSDictionary *)dic;
+ (instancetype)provinceWithDic:(NSDictionary *)dic;
@end
#import "Province.h"
@implementation Province
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
- (instancetype)initWithDic:(NSDictionary *)dic
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+ (instancetype)provinceWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
@end
#import "ViewController.h"
#import "Province.h"
@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
//数据源
@property (strong, nonatomic)NSArray *provinceData;
@property (strong, nonatomic)UIPickerView *pickView;
//显示选中的结果
@property (strong, nonatomic)UILabel *cityLabel;
@end
@implementation ViewController
//使用NSArray方式别人更改数据
- (NSArray *)provinceData
{
if (_provinceData == nil) {
NSMutableArray *provinceArr = [NSMutableArray array];
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil]];
for (NSDictionary *dic in array) {
Province *p = [Province provinceWithDic:dic];
[provinceArr addObject:p];
}
_provinceData = provinceArr;
}
return _provinceData;
}
- (void)viewDidLoad {
[super viewDidLoad];
/*
UIPickerView的属性和方法
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; // 数据源
@property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate; // 代理
@property(nonatomic) BOOL showsSelectionIndicator; // 是否显示指示器,默认是NO
@property(nonatomic,readonly) NSInteger numberOfComponents; //列的个数
- (NSInteger)numberOfRowsInComponent:(NSInteger)component; //列中的行数
- (CGSize)rowSizeForComponent:(NSInteger)component; //列中行的尺寸
// 返回一个自定义的view,如选择国旗,view可以是:国家名+国旗
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
- (void)reloadAllComponents; // 刷新全部列
- (void)reloadComponent:(NSInteger)component; //刷新选中的列
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // 选中时的动画效果
- (NSInteger)selectedRowInComponent:(NSInteger)component; //获取选中列中的行号
*/
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
//设置label
self.cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, screenW, 30)];
self.cityLabel.backgroundColor = [UIColor clearColor];
self.cityLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.cityLabel];
self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 150, screenW, 200)];
self.pickView.delegate = self;
self.pickView.dataSource = self;
self.pickView.showsSelectionIndicator = YES;
[self.view addSubview:self.pickView];
//默认选中第0行0列 compmnent:列数 row:每列对应的行数
[self pickerView:self.pickView didSelectRow:0 inComponent:0];
}
数据源和代理方法
//多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//每列对应的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {//多少个省份
return self.provinceData.count;
}else{
//当前选中的哪个省
//选中第一列中的行号
NSInteger pIndex = [pickerView selectedRowInComponent:0];
//取出对应的省会
Province *p = self.provinceData[pIndex];
return p.cities.count;
}
}
//代理
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
Province *p = self.provinceData[row];
return p.name;
} else {
NSInteger pIndex = [pickerView selectedRowInComponent:0];
Province *p = self.provinceData[pIndex];
return p.cities[row];
}
}
//监听选中的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//选中第一列
if (component == 0) {
//刷新第二列的数据
[pickerView reloadComponent:1];
//选择第二列中的第一行(默认显示)
[pickerView selectRow:0 inComponent:1 animated:YES];
}
//获取选中的省 份名称
NSInteger pIndex = [pickerView selectedRowInComponent:0];
Province *p = self.provinceData[pIndex];
//获取选中的城市名称
NSInteger cIndex = [pickerView selectedRowInComponent:1];
self.cityLabel.text = [NSString stringWithFormat:@"%@ %@", p.name, p.cities[cIndex]];
}