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);

        }

    }

     
  • 相关阅读:
    Fork/Join框架基本使用
    服务端高并发分布式架构演进之路
    Netty专题(一)-----计算机网络、TCP/ICP、linux网络I/O模型
    Nginx专题(四)-----https、nginx高可用
    Nginx专题(三)-----核心原理、跨域解决、防盗链、缓存以及压缩
    微信开放平台开发第三方授权登陆(四):微信小程序
    微信开放平台开发第三方授权登陆(三):微信公众号
    微信开放平台开发第三方授权登陆(二):PC网页端
    微信开放平台开发第三方授权登陆(一)-----开发前准备
    Mysql:如果数据存在则更新,不存在则插入
  • 原文地址:https://www.cnblogs.com/wujie123/p/5269738.html
Copyright © 2011-2022 走看看