zoukankan      html  css  js  c++  java
  • IOS简单的选择器实现UIPickerView(省+市+区)

    轮子的样式:

    area.plist的样式:

    1.解析文件

    1.1解析省

     1 -(void)ProvinceArray
     2 {
     3     
     4     //获取第一层Dictionary
     5     self.allDic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"]];
     6     self.provinceArray = [[NSMutableArray alloc]init];
     7     for(int i = 0; i < self.allDic.count; i++)
     8     {
     9         [self.provinceArray addObject:[[[self.allDic objectForKey:[NSString stringWithFormat:@"%d",i]] allKeys] objectAtIndex:0]];
    10     }
    11 }

    1.2解析市

     1 -(void)CityArray:(NSInteger)row
     2 {
     3     //获取第三层Dictionary
     4     self.province = [[self.allDic objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.provinceArray[row]];
     5     self.cityArray = [[NSMutableArray alloc]init];
     6     for(int i = 0;i < self.province.count; i++)
     7     {
     8         [self.cityArray addObject:[[[self.province objectForKey:[NSString stringWithFormat:@"%d",i]] allKeys] objectAtIndex:0]];
     9     }
    10 }

    1.3解析区

    1 -(void)CountyArray:(NSInteger)row
    2 {
    3     self.countyArray = [[NSMutableArray alloc]init];
    4     self.countyArray = [[self.province objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.cityArray[row]];
    5 }

    2.绑定并初始化UIPickerView

     1 //以下3个方法实现PickerView的数据初始化
     2 //确定picker的轮子个数
     3 #pragma mark 实现协议UIPickerViewDataSource的方法
     4 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
     5 {
     6     //设置几个滚轮
     7     return 3;
     8 }
     9 //确定picker的每个轮子的item数
    10 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    11 {
    12     switch (component) {
    13         case 0://省份个数
    14             return [self.provinceArray count];
    15             break;
    16         case 1://市的个数
    17             return [self.cityArray count];
    18             break;
    19         case 2://县的个数
    20             return [self.countyArray count];
    21             break;
    22         default:
    23             break;
    24     }
    25     return 0;
    26 }
    27 //确定每个轮子的每一项显示什么内容
    28 #pragma mark 实现协议UIPickerViewDelegate的方法
    29 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    30 {
    31     switch (component) {
    32         case 0://选择省份
    33             return [self.provinceArray objectAtIndex:row];
    34             break;
    35         case 1://选择市
    36             return [self.cityArray objectAtIndex:row];
    37             break;
    38         case 2://选择县
    39             return [self.countyArray objectAtIndex:row];
    40             break;
    41         default:
    42             break;
    43     }
    44     return 0;
    45 }

    3.监听轮子的滚动

     1 //监听轮子的移动
     2 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
     3 {
     4     if (component == 0) {
     5         [self CityArray:row];
     6         [self CountyArray:0];
     7         [self.pickerView reloadComponent:1];
     8         [self.pickerView reloadComponent:2];
     9     }
    10     if (component == 1) {
    11         [self CountyArray:row];
    12         [self.pickerView reloadComponent:2];
    13     }
    14 }

    程序代码+area.plist文件 http://files.cnblogs.com/files/guoyongzhi/PickerView.zip

  • 相关阅读:
    STL特性总述——写在前面
    C++多线程框架
    C++内存管理之unique_ptr
    ubuntu文本模式/终端中文乱码解决
    log4net日志在app.config中assembly不起作用
    解决多线程委托二义性问题
    IIS 中文文件名下载会出现403访问被拒绝
    C# 异常:从作用域“”引用了“FiasHostApp.Entity.DBEntity.FIAS_RM_v1.ITraNetMgrUnitBaseInfoRecord”类型的变量“w”,但该变量未定义
    C# string.Split对于换行符的分隔正确用法
    knockoutJS+knockout.multimodels使用记录
  • 原文地址:https://www.cnblogs.com/guoyongzhi/p/4765549.html
Copyright © 2011-2022 走看看