zoukankan      html  css  js  c++  java
  • 用UIpickView实现省市的联动

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

    @property(strong,nonatomic)UIPickerView *pickView;

    //定义一个可变数组用于存放省的数据

    @property(strong,nonatomic)NSMutableArray *Statearry;

    //定义一个可变数组用于存放市的数据

    @property(strong,nonatomic)NSMutableArray *Citiesarry;

    //定义一个集合分别存省和市的数据

    @property(strong,nonatomic)NSArray *arry;

    @end

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        //获取数据

        NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];    

        //初始化省和市的数组

        self.Statearry=[NSMutableArray array];

        self.Citiesarry=[NSMutableArray array];

        //ayyr这个大数组存放所有的省市

        self.arry=[NSArray arrayWithContentsOfFile:path];

        //获取省份的,将取出来的省份数据放在省的可变集合Statearry里

        for (NSDictionary *arr in self.arry)

        {

            [self.Statearry addObject:arr[@"State"]];

        }

        

        

        //创建pickView

        self.pickView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 414, 200)];

        self.pickView.backgroundColor=[UIColor grayColor];

        

        self.pickView.delegate=self;

        self.pickView.dataSource=self;

        [self.view addSubview:self.pickView]; 

    #pragma mark 数据源 Method numberOfComponentsInPickerView

     - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    {

        //两列

        return 2;

    }

    #pragma mark 数据源 Method pickerViewOfRows

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

    {

        if (component==0)

        {

            //省份的个数

            return self.Statearry.count;

        }

        else

        {

            //市的个数

            return self.Citiesarry.count;

        }

    }

     #pragma mark delegate 显示信息的方法

    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

    {

        if (component==0)

        {

            //选择的省份

            return self.Statearry[row];

        }

        else

        {

            //选择的市

            return self.Citiesarry[row];

        }  

    }

    #pragma mark 选中行的信息

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    {

        if (component==0)

        {

            

            //清空上一次市的那一列留下来的数据

            [self.Citiesarry removeAllObjects];

            //定义一个index,找出第一个滚动条里面的所有省对应的下标找出来,赋值给index

            NSInteger index=[pickerView selectedRowInComponent:0];

            //遍历出所有市

            for (NSDictionary *city in self.arry[index][@"Cities"])

            {

                //将遍历出来市追加到存放市的集合里

                [self.Citiesarry addObject:city[@"city"]];

            }

    //        NSLog(@"%@",self.Citiesarry);

        

            //更新第二个滚轮的数据

            [self.pickView reloadComponent:1];

        }

        else

        {

            //显示取出来的省和市

            NSString *message=[NSString stringWithFormat:@"你选择的是%@的%@?",self.Statearry[[pickerView selectedRowInComponent:0]],self.Citiesarry[row]];

            

            //设置弹出框的标题

            UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

            

            //设置按钮名称

            UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                

            }];

            //设置按钮名称

            UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

            //将按钮加到提示框里面

            [alert addAction:okAction];

            [alert addAction:cancelAction];

            //

            [self presentViewController:alert animated:YES completion:nil];

        }       

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

  • 相关阅读:
    Jmeter(二十七) 从入门到精通 Jmeter Http协议录制脚本(详解教程)
    Jmeter(二十六) 从入门到精通 搭建开源论坛JForum(详解教程)
    [Erlang0003][OTP] Efficiency Guide User's Guide > Common Caveats
    [Erlang0008][OTP] 高效指南 表和数据库(ets mnesia)
    [Erlang0004][OTP] 高效指南 二进制的构造和匹配(1)
    [Erlang0002][OTP] Efficiency Guide User's Guide > The Eight Myths of Erlang Performance
    [Erlang0010][News]OTP 技术委员会 影响R16的决策 (OTP Technical Board Decisions affecting R16 翻译)
    [Erlang0007][OTP] 高效指南 函数
    [Erlang0005][OTP] 高效指南 二进制的构造和匹配(2)
    [Erlang0001][OTP] Efficiency Guide User's Guide>Introduction
  • 原文地址:https://www.cnblogs.com/layios/p/5270277.html
Copyright © 2011-2022 走看看