iOS UIPickerView 显示全国省市
效果图



#import "ViewController.h" @interface ViewController ()
@property(strong,nonatomic)UIPickerView *myPickView1;
@property(strong,nonatomic)NSArray *guo;
@property(strong,nonatomic)NSMutableArray *sheng;
@property(strong,nonatomic)NSMutableArray *shixian;
@property(strong,nonatomic)NSDictionary *shi;
@property(strong,nonatomic)UIButton *btn;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.sheng=[NSMutableArray array];
self.shixian=[NSMutableArray array];
// self.guo=[NSArray array]; //添加UIPickerView
self.myPickView1=[[UIPickerView alloc]initWithFrame:CGRectMake(10, 100, 400, 300)];
self.myPickView1.backgroundColor=[UIColor colorWithRed:0.945 green:1.000 blue:0.359 alpha:1.000];
self.myPickView1.delegate=self;
self.myPickView1.dataSource=self; //读取plist文件
NSBundle *bundle=[NSBundle mainBundle];
NSString *path=[bundle pathForResource:@"area.plist" ofType:nil];
self.guo=[NSArray arrayWithContentsOfFile:path]; //遍历 省
for (NSDictionary *arr in self.guo) {
[self.sheng addObject:arr[@"State"]];
} [self.view addSubview:self.myPickView1]; } #pragma mark - 数据源
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;
} #pragma mark - 数据源,返回长度
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==FIRST) {
return self.sheng.count;
}
return self.shixian.count;
} #pragma mark - 显示信息
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component==FIRST) {
return [self.sheng objectAtIndex:row];
} return [self.shixian objectAtIndex:row]; } #pragma mark - 选中选的信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==FIRST) {
//清除上次选择的城市
[self.shixian removeAllObjects];
//更具城市的索引,遍历串城市
NSDictionary *a=[self.guo objectAtIndex:row];
//遍历城市信息
NSArray *array=a[@"Cities"];
NSMutableArray *s=[NSMutableArray array];
for (NSDictionary *a in array) {
[s addObject:a[@"city"]];
}
//把值传给 self.shixian
self.shixian=s; //把第一个加到第二个中
[self.myPickView1 selectRow:row inComponent:FIRST animated:YES]; //刷新组件
[self.myPickView1 reloadComponent:SECOND]; }
//如果选中第二个,弹出信息
if (component==SECOND) {
NSInteger firstRow=[self.myPickView1 selectedRowInComponent:FIRST];
NSInteger secondRow=[self.myPickView1 selectedRowInComponent:SECOND]; NSString *firstString=[self.sheng objectAtIndex:firstRow];
NSString *secondString=[self.shixian objectAtIndex:secondRow]; NSString *message=[NSString stringWithFormat:@"您确定要选择%@%@吗?",firstString,secondString];
UIAlertController *alertMessage=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alertMessage addAction:cancle];
[alertMessage addAction:ok];
[self presentViewController:alertMessage animated:YES completion:nil];
}
} #pragma mark - 行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 30;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

ViewController.h文件

#import <UIKit/UIKit.h>
#define FIRST 0
#define SECOND 1
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> @end

注:plist文件结构

iOS UIPickerView 显示全国省市的更多相关文章
- 全国省市级联数据sql语句 mysql版
全国省市级联数据sql语句 mysql版 --省级 provincial create table provincial ( provincialID int, provincialName ), p ...
- IOS 状态栏 显示与隐藏网络活动状态
IOS中显示和隐藏状态栏的网络活动标志 //在向服务端发送请求状态栏显示网络活动标志: [[UIApplication sharedApplication] setNetworkActivityInd ...
- vue中解决时间在ios上显示NAN的问题
最近在用vue,遇到倒计时在ios上显示为NAN的问题. 因为做的是倒计时支付,思路是获取服务器时间和下单时间,再转成秒级时间戳做差值. 在网上找到说是ios 不支持例如2018-09-01 10:0 ...
- iOS - UIPickerView
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITa ...
- ios日期显示NaN
ios中js通过getMonth()获取到的日期显示NaN,而在其他地方如pc.安卓都是ok的,这是为什么呢,原来这里有个ios的兼容问题,需要将日期中的“-”替换为“/” var time = ne ...
- iOS:UIPickerView选择器的使用
通过UIPickerView选择器做的一个类似于密码锁的日期时间表 源码如下: #import <UIKit/UIKit.h> @interface ViewController : UI ...
- ios上传图片显示方向错误问题
IOS 上传图片方向显示错误问题 问题描述 在使用苹果手机上传图片的时候,发现传完的图片显示出来方向是错误的,竖着的图片会变成横着显示(少部分安卓手机也存在这个问题) 产生原因 ios 相机加入了方向 ...
- iOS 状态栏显示/隐藏
iOS 10为止,将在plist文件中将 View controller-based status bar appearance 设置为NO,就可以用UIApplication设置状态栏. 注意,要在 ...
- 全国省市区县和乡镇街道行政区划矢量边界坐标经纬度地图最新数据免费下载 支持shp geojson json sql格式
关键词: 省市区三级, 乡镇四级, 全国, 行政区划, 坐标边界, 矢量数据, 地理围栏, 免费下载, 2018 2019 2020 2021 2022年份, 最新数据, 长期更新, 开源维护, 支持 ...
随机推荐
- 安装LoadRunner提示缺少vc2005_sp1_with_atl..
装自动化负载测试工具LoadRunner前,需要预先安装其运行的基础环境.如:安装LoadRunner 11时就需要先安装Micrsoft Visual C++ 2005 SP1.C++ 2008运行 ...
- sql2008r2数据库附加的问题
sql2008r2数据库附加,一般都没有问题,但是偶尔也会出错,无法附加,一般的原因都是权限不够,主要是:Authenticated Users要开通完全控制功能,选中该用户(如果没有该用户,就添加) ...
- 获取指定DLL程序集Config 文件
/// <summary> /// 获取调用函数所在程序集的配置信息 /// </summary> /// <returns></returns> pr ...
- 安卓自动化测试:Android studio 自带的 Record Espresso Test || [ Appium & (Android studio || Python|| Eclipse ) ]
1.Android studio 自带的 Record Espresso Test https://developer.android.com/studio/test/espresso-test-r ...
- 【译】Permissions Best Practices Android M权限最佳做法
Permissions Best Practices PreviousNext In this document Consider Using an Intent Don't Overwhelm th ...
- 【初级】linux cp 命令详解及使用方法实战
cp:复制文件或者目录 前言: cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不 ...
- ASP.NET4.5从零开始(1)
使用Web窗体 引言 前段时间客户A突然提出,B项目希望可以做成BS形式的,之前是CS的,因为我之前也没学过ASP.NET,于是一边百度,一边Coding,马马虎虎的把功能流程给调通,然后就交差了,想 ...
- java多线程之 基本概念
一.线程的五种状态 1. 新建状态(New) : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状态(Runnable) ...
- mac brew mysql 启动之后报错
打开电脑 链接mysql 发现报错,连不上,应该是没自启动, 之前一直用windows电脑,就用mysql start 准备启动下,发现报错, p.p1 { margin: 0.0px 0.0px 0 ...
- VMware下利用ubuntu13.04建立嵌入式开发环境之五
tftp和nfs服务器配置 一.tftp服务器配置 1.安装软件包 1.1安装服务 apt-get install tftpd-hpa 1.2安装客户端 apt-get install tftp-hp ...