省市县三级联动(PickerViewDemo)

一、项目结构


二、plist文件结构



三、代码实现

//
//  ViewController.m
//  PickerViewDemo
//
//  Created by 蜂蜜哦 on 06/11/15.
//  Copyright (c) 2015年 蜂蜜哦. All rights reserved.
//

#import "ViewController.h"

#define FirstComponent 0
#define SubComponent 1
#define ThirdComponent 2
#define WIDTH self.view.frame.size.width/3.5;
#define HEIGHT self.view.frame.size.height/5;

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIPickerView *picker;
    NSDictionary *dict;
    NSArray *pickerArray;
    NSArray *subPickerArray;
    NSArray *thirdPickerArray;
    NSString *province; //省
    NSString *city; //市
    NSString *county; //县
    NSString *selectContent; //选择的省市县
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"省市区";
    
    //初始化数据
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    pickerArray = [dict allKeys];
    province = pickerArray[0];
    if (dict[province]!= nil) {
        subPickerArray = [dict[province] allKeys];
        city = subPickerArray[0];
    } else {
        subPickerArray = nil;
    }
    if (dict[province][city]!=nil) {
        thirdPickerArray = dict[province][city];
    } else {
        thirdPickerArray = nil;
    }
                
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 400, 100, 40)];
    sureBtn.backgroundColor = [UIColor colorWithRed:1.000 green:0.230 blue:0.346 alpha:1.000];
    [sureBtn setTitle:@"选择省市区" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureSelect) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
}

#pragma mark - 确定选择按钮监听
- (void)sureSelect {
    //  Portrait 表示 纵向,Landscape 表示 横向。
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n";
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        province = pickerArray[[picker selectedRowInComponent:FirstComponent]];
        city = subPickerArray[[picker selectedRowInComponent:SubComponent]];
        county = thirdPickerArray[[picker selectedRowInComponent:ThirdComponent]];
        if (city==nil) {
            selectContent = [NSString stringWithFormat:@"%@",province];
        } else if(county==nil) {
            selectContent = [NSString stringWithFormat:@"%@-%@",province,city];
        } else {
            selectContent = [NSString stringWithFormat:@"%@-%@-%@",province,city,county];
        }
        self.title = selectContent;
    }];
    
    //初始化UIPickerView
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-20, 200)];
    picker.delegate = self;
    picker.dataSource = self;
    
    [alertController.view addSubview:picker];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
                
#pragma mark - UIPickerViewDelegate、UIPickerViewDataSource协议
//确定有几组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

//确定每组的元素个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case FirstComponent:
            return pickerArray.count;
            break;
        case SubComponent:
            return subPickerArray.count;
            break;
        case ThirdComponent:
            return thirdPickerArray.count;
            break;
        default:
            break;
    }
    return 0;
}

//宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return WIDTH;
}

//绑定数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case FirstComponent:
            return pickerArray[row];
            break;
        case SubComponent:
            return subPickerArray[row];
            break;
        case ThirdComponent:
            return thirdPickerArray[row];
            break;
        default:
            break;
    }
    return nil;
}

//数据选择控制
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case FirstComponent:
            province = pickerArray[row];
            if (dict[province]!=nil) {
                subPickerArray = [dict[province] allKeys];
                city = subPickerArray[0];
                if (dict[province][city]!=nil) {
                    thirdPickerArray = dict[province][city];
                } else {
                    thirdPickerArray = nil;
                }
            } else {
                subPickerArray = nil;
                thirdPickerArray = nil;
            }
            [picker selectedRowInComponent:SubComponent];
            [picker reloadComponent:SubComponent];
            [picker selectedRowInComponent:ThirdComponent];
            [picker reloadComponent:ThirdComponent];
            break;
        case SubComponent:
            city = subPickerArray[row];
            if (dict[province][city]!=nil) {
                thirdPickerArray = dict[province][city];
            } else {
                thirdPickerArray = nil;
            }
            [picker selectRow:0 inComponent:ThirdComponent animated:YES];
            [picker reloadComponent:ThirdComponent];
            break;
        default:
            break;
    }
}


@end


四、实现效果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值