uipicker对省市的遍历

1、首先,对UIPickerView绑定Delegate和DataSource到相应的ViewController。此处不再赘述。可以用代码或者Interface界面设置。

2、首先实现数据的初始化。

(1)在.h文件中定义如下变量。其中provinces_cities.plist请见附件。

Cpp代码  收藏代码
  1. @interface IkrboyViewController : UIViewController{  
  2.     NSDictionary *dict;//用于存储省份-城市的数据  
  3.     NSArray *provinceArray;//省份的数组  
  4.     NSArray *cityArray;//城市的数组,在接下来的代码中会有根据省份的选择进行数据更新的操作  
  5. }  

 (2)在.m的viewDidLoad方法中加上初始化数据的处理。具体处理在initPicker方法
 

Cpp代码  收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     [self initPicker];  
  6. }  
  7.   
  8. //初始化PickerView使用的数据源  
  9. -(void)initPicker{  
  10.     NSBundle *bundle = [NSBundle mainBundle];  
  11.     NSString *plistPath = [bundle pathForResource:@"provinces_cities" ofType:@"plist"];  
  12.     dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];  
  13.     provinceArray = [dict allKeys];  
  14.      
  15.     NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];  
  16.     NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];  
  17.     cityArray = [dict objectForKey:seletedProvince];  
  18.     NSLog(@"%d",[provinceArray count]);  
  19. }  

 3、将数据绑定到UIPickerView

Cpp代码  收藏代码
  1. //以下3个方法实现PickerView的数据初始化  
  2. //确定picker的轮子个数  
  3. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {  
  4.     return 2;  
  5. }  
  6. //确定picker的每个轮子的item数  
  7. - (NSInteger)pickerView:(UIPickerView *)pickerView  
  8. numberOfRowsInComponent:(NSInteger)component {  
  9.     if (component == 0) {//省份个数  
  10.         return [provinceArray count];  
  11.     } else {//市的个数  
  12.         return [cityArray count];  
  13.     }  
  14. }  
  15. //确定每个轮子的每一项显示什么内容  
  16. #pragma mark 实现协议UIPickerViewDelegate方法  
  17. -(NSString *)pickerView:(UIPickerView *)pickerView  
  18.             titleForRow:(NSInteger)row forComponent:(NSInteger)component {  
  19.     if (component == 0) {//选择省份名  
  20.         return [provinceArray objectAtIndex:row];  
  21.     } else {//选择市名  
  22.         return [cityArray objectAtIndex:row];  
  23.     }  
  24. }  

 4.随时监听UIPickerView的滚动。

Cpp代码  收藏代码
    1. //监听轮子的移动  
    2. - (void)pickerView:(UIPickerView *)pickerView  
    3.       didSelectRow:(NSInteger)row inComponent:(NSInteger)component {  
    4.     if (component == 0) {  
    5.         NSString *seletedProvince = [provinceArray objectAtIndex:row];  
    6.         cityArray = [dict objectForKey:seletedProvince];  
    7.           
    8.         //重点!更新第二个轮子的数据  
    9.          [self.pickerView reloadComponent:1];  
    10.           
    11.         NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];  
    12.         NSString *seletedCity = [cityArray objectAtIndex:selectedCityIndex];  
    13.           
    14.         NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];  
    15.         NSLog(@"%@",msg);  
    16.     }  
    17.     else {  
    18.         NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];  
    19.         NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];  
    20.           
    21.         NSString *seletedCity = [cityArray objectAtIndex:row];  
    22.         NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];  
    23.         NSLog(@"%@",msg);  
    24.     }  
    25. }  
    26. 附件下载地址:

      http://files.cnblogs.com/files/anjiubo/city.plist.zip

原文地址:https://www.cnblogs.com/anjiubo/p/5293378.html