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

  • 相关阅读:
    Appium常用操作之混合应用H5上下文切换
    Appium常用操作之X5内核应用(如微信小程序)上下文切换
    Appium常用操作之toast弹出框处理
    Appium常用操作之页面中的滑动点击
    Appiun常用操作之页面滑动操作
    Appium常用操作之九宫格滑动解锁
    Git、GitHub、GitLab三者之间的联系以及区别
    vm上如何安装centos7
    harles 抓web http、https请求,需要怎么做?给电脑以及浏览器安装证书并设置host、端口号
    Spring Boot Admin开源监控系统
  • 原文地址:https://www.cnblogs.com/layios/p/5270277.html
Copyright © 2011-2022 走看看