zoukankan      html  css  js  c++  java
  • 省市便利 UIPicherView

    @property (strong,nonatomic) UIPickerView *pickerV;

    @property (strong,nonatomic) NSArray *arr;

    @property (strong,nonatomic) NSMutableArray *arrCity;

    @property (strong,nonatomic) NSMutableArray *arrPro;

    @property (assign,nonatomic) int index;

    @property (strong,nonatomic) NSString *provice;

    @property (strong,nonatomic) NSString *city;

    @property (strong,nonatomic) UIButton *btn;

     
     

     self.pickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(40, 100, 300, 200)];

        self.pickerV.backgroundColor = [UIColor grayColor];

        self.pickerV.delegate = self;

        self.pickerV.dataSource = self;

        [self.view addSubview:self.pickerV];

        

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

        self.arr = [NSArray array];

        self.arr = [NSArray arrayWithContentsOfFile:path];

        self.arrCity = [NSMutableArray array];

        self.arrPro = [NSMutableArray array];

        

        for (NSDictionary *dic in self.arr)

        {

            [self.arrPro addObject:dic[@"State"]];

            

        }

        self.index = (int)[self.pickerV selectedRowInComponent:0];

        NSDictionary *dic1 = [self.arr objectAtIndex:self.index];

        [self.arrCity addObjectsFromArray:dic1[@"Cities"]];

        

        

        self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        self.btn.backgroundColor = [UIColor blueColor]; 

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    {

        return 2;

    }

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

    {

        if (component == 0)

        {

          return self.arrPro.count;

        }

        

        else

        {

            return self.arrCity.count;

        }

    }

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

    {

        if (component == 0)

        {

            return self.arrPro[row];

        }

        else

        {

            return self.arrCity[row][@"city"];

        }

    }

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

    {

        if (component == 0)

        {

            [self.arrCity removeAllObjects];

            NSString *provice = [self.arrPro objectAtIndex:row];

            [self.arrCity addObjectsFromArray:self.arr[row][@"Cities"]];

            [self.pickerV reloadComponent:1];

            NSInteger cityIndex = [self.pickerV selectedRowInComponent:1];

            NSString *city = [self.arrCity objectAtIndex:cityIndex];

            NSString *msg = [NSString stringWithFormat:@"%@,%@",provice,city];

            NSLog(@"%@",msg);

        }

        

        else

        {

            NSInteger proviceIndex = [self.pickerV selectedRowInComponent:0];

            NSString *provice = [self.arrPro objectAtIndex:proviceIndex];

            NSString *city = [self.arrCity objectAtIndex:row];

            NSString *msg = [NSString stringWithFormat:@"%@,%@",provice,city];

            NSLog(@"%@",msg);

        }

    }

     
  • 相关阅读:
    使用60赫兹交流电的关西人
    UI交互细节节选控件使用细则
    PNG button (按钮) files with transparency, for Visual C++ 6.0 and VS2005
    成都第二天:美食
    精力管理与状态转换
    再议“专注”
    成都第一天:印象
    Flash AMF协议
    AS3.0 JSON介绍
    AS3.0 利用AMFPHP与PHP进行通讯 .
  • 原文地址:https://www.cnblogs.com/wujie123/p/5269738.html
Copyright © 2011-2022 走看看