标签:
1 // 2 // JWProvince.h 3 // 12-27-Province 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface JWProvince : NSObject 12 @property (nonatomic,copy) NSString *name; 13 @property (nonatomic,strong) NSArray *cities; 14 - (instancetype)initWithDic:(NSDictionary *)dic; 15 + (instancetype)provinceWithDic:(NSDictionary *)dic; 16 + (NSMutableArray *)provinceList; 17 @end
1 // 2 // JWProvince.m 3 // 12-27-Province 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import "JWProvince.h" 10 11 @implementation JWProvince 12 - (instancetype)initWithDic:(NSDictionary *)dic { 13 if (self = [super init]) { 14 [self setValuesForKeysWithDictionary:dic]; 15 } 16 return self; 17 } 18 + (instancetype)provinceWithDic:(NSDictionary *)dic { 19 return [[self alloc] initWithDic:dic]; 20 } 21 22 + (NSMutableArray *)provinceList { 23 NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"provinces" ofType:@"plist"]]; 24 25 NSMutableArray *tempArray = [NSMutableArray array]; 26 27 for (NSDictionary *dic in array) { 28 JWProvince *province = [JWProvince provinceWithDic:dic]; 29 [tempArray addObject:province]; 30 } 31 return tempArray; 32 } 33 @end
Controller:
1 // 2 // ViewController.m 3 // 12-27-Province 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "JWProvince.h" 11 @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> 12 @property (nonatomic,strong) NSArray *provinceDatas; 13 @property (nonatomic,assign) NSInteger index; 14 @end 15 16 @implementation ViewController 17 #pragma mark - 懒加载 18 - (NSArray *)provinceDatas { 19 if (!_provinceDatas) { 20 _provinceDatas = [JWProvince provinceList]; 21 } 22 return _provinceDatas; 23 } 24 #pragma mark - UIPickerView数据源 25 //返回显示数据的列数 26 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 27 return 2; 28 } 29 //返回每列中对应的行的数 30 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 31 if (component == 0) { 32 return self.provinceDatas.count;//第一列显示的个数 33 } 34 JWProvince *pro = self.provinceDatas[self.index]; 35 return pro.cities.count;//第二列显示的个数 36 } 37 #pragma mark - UIPickerView代理 38 //返回显示每列以及每行的内容 39 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 40 if (component == 0) { 41 JWProvince *pp = self.provinceDatas[row]; 42 return pp.name;//第一列显示的内容 43 } 44 JWProvince *pro = self.provinceDatas[self.index]; 45 return pro.cities[row];//第二列显示的内容 46 } 47 #pragma mark - pickerView的选中方法 48 //选中行时做的操作 49 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 50 if (component == 0) { 51 self.index = row; 52 [pickerView reloadComponent:1];//重新加载对应的数据 53 [pickerView reloadAllComponents];//重新加载所有的数据 54 [pickerView selectRow:0 inComponent:1 animated:YES]; //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行 55 } 56 } 57 @end
标签:
原文地址:http://www.cnblogs.com/xiaomoge/p/4200831.html