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年份, 最新数据, 长期更新, 开源维护, 支持 ...
随机推荐
- golang使用 mongo
连接集群 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?op ...
- 通过Maven插件发布JaveEE项目到tomcat下
1.修改tomcat\conf\tomcat-users.xml文件,在文件中增加 <role rolename="manager-script"/> <user ...
- java selenium (六) XPath 定位
xpath 的定位方法, 非常强大. 使用这种方法几乎可以定位到页面上的任意元素. 阅读目录 什么是xpath xpath 是XML Path的简称, 由于HTML文档本身就是一个标准的XML页面, ...
- yii2 框架的 save() 方法 执行模式条件。
save() 方法会调用 insert() 和 update() 中的一个, 用哪个取决于当前 AR 对象是不是新对象(在函数内部,他会检查 yii\db\ActiveRecord::isNewRe ...
- Nuget包之间的依赖
为什么我们使用依赖呢??原因是某些资源是基于某些资源的基础上才可以运行的,比如bootstrap基于Jquery,EntityFramework.zh-Hans基于EntityFramework,如果 ...
- Android网络连接判断与处理
博客分类: Android 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android ...
- css 变量与javascript结合
<div onClick="test('yellow')"> CSS Variable</div> ================CSS :root{ - ...
- 初学js
接触时感觉跟前面写网页的差距和大,与c语言很相似.主要学的有: 1.引入js的三种方法:外联,内联,嵌套 2.标识符:第一个字符可以是任意Unicode字母,以及美元符号($)和下划线(_). - 第 ...
- 用PHP抓取淘宝商品的用户晒单评论+图片实例
为什么想起来做这个功能?是因为前段时间在做一个淘客网站的时候,想到是否能抓取到淘宝商品的买家秀呢?经过一番折腾发现,淘宝商品用户评价信息是通过Ajax来调取的,通过嗅探网址发现,评论数据的请求接口是: ...
- char* 和char[]的区别
以下内容均来自互联网,系笔者汇总并总结. 1. 问题介绍 问题引入:在实习过程中发现了一个以前一直默认的错误,同样char *c = "abc"和char c[]="ab ...