zoukankan      html  css  js  c++  java
  • iOS UIPickerView 显示全国省市

    效果图

    #import "ViewController.h"
    
    @interface ViewController ()
    @property(strong,nonatomic)UIPickerView *myPickView1;
    @property(strong,nonatomic)NSArray *guo;
    @property(strong,nonatomic)NSMutableArray *sheng;
    @property(strong,nonatomic)NSMutableArray *shixian;
    @property(strong,nonatomic)NSDictionary *shi;
    @property(strong,nonatomic)UIButton *btn;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.sheng=[NSMutableArray array];
        self.shixian=[NSMutableArray array];
    //    self.guo=[NSArray array];
    
        //添加UIPickerView
        self.myPickView1=[[UIPickerView alloc]initWithFrame:CGRectMake(10, 100, 400, 300)];
        self.myPickView1.backgroundColor=[UIColor colorWithRed:0.945 green:1.000 blue:0.359 alpha:1.000];
        self.myPickView1.delegate=self;
        self.myPickView1.dataSource=self;
    
        //读取plist文件
        NSBundle *bundle=[NSBundle mainBundle];
        NSString *path=[bundle pathForResource:@"area.plist" ofType:nil];
       self.guo=[NSArray arrayWithContentsOfFile:path];
        
        //遍历  省
        for (NSDictionary *arr in self.guo) {
          [self.sheng addObject:arr[@"State"]];
        }
        
        
        [self.view addSubview:self.myPickView1];
    
        
    }
    
    
    
    #pragma mark - 数据源
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
        return 2;
    }
    
    #pragma mark - 数据源,返回长度
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        if (component==FIRST) {
             return self.sheng.count;
        }
        return self.shixian.count;
    }
    
    #pragma mark - 显示信息
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        if (component==FIRST) {
            return [self.sheng objectAtIndex:row];
        }
       
    
            return [self.shixian objectAtIndex:row];
        
        
    }
    
    #pragma mark - 选中选的信息
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        
        if (component==FIRST) {
            //清除上次选择的城市
            [self.shixian removeAllObjects];
            //更具城市的索引,遍历串城市
            NSDictionary *a=[self.guo objectAtIndex:row];
            //遍历城市信息
            NSArray *array=a[@"Cities"];
            NSMutableArray *s=[NSMutableArray array];
            for (NSDictionary *a in array) {
                [s addObject:a[@"city"]];
            }
            //把值传给 self.shixian
            self.shixian=s;
            
            //把第一个加到第二个中
            [self.myPickView1 selectRow:row inComponent:FIRST animated:YES];
            
            //刷新组件
            [self.myPickView1 reloadComponent:SECOND];
        
        }
        //如果选中第二个,弹出信息
        if (component==SECOND) {
            NSInteger firstRow=[self.myPickView1 selectedRowInComponent:FIRST];
            NSInteger secondRow=[self.myPickView1 selectedRowInComponent:SECOND];
            
            NSString *firstString=[self.sheng objectAtIndex:firstRow];
            NSString *secondString=[self.shixian objectAtIndex:secondRow];
            
            
            NSString *message=[NSString stringWithFormat:@"您确定要选择%@%@吗?",firstString,secondString];
            UIAlertController *alertMessage=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            
            [alertMessage addAction:cancle];
            [alertMessage addAction:ok];
            [self presentViewController:alertMessage animated:YES completion:nil];
        }
    }
    
    #pragma  mark - 行高
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
        return 30;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    ViewController.h文件

    #import <UIKit/UIKit.h>
    #define FIRST 0
    #define SECOND 1
     @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
    
    @end

    注:plist文件结构

  • 相关阅读:
    android开发系列之gradle认识
    angularjs+nodejs+mongodb三件套
    我对服务端开发的一些认识
    近几个月的技术总结
    IOS设计模式的六大设计原则之单一职责原则(SRP,Single Responsibility Principle)
    第二阶段第八次站立会议
    第二阶段第七次站立会议
    第二阶段第六次站立会议
    第二阶段第五次站立会议
    第二阶段第四次站立会议
  • 原文地址:https://www.cnblogs.com/qianLL/p/5267381.html
Copyright © 2011-2022 走看看