省市便利 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);

    }

}

 
posted @ 2016-03-12 19:09  原码  阅读(131)  评论(0编辑  收藏  举报