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年份, 最新数据, 长期更新, 开源维护, 支持 ...
随机推荐
- js面向对象的使用方法
标准用法: function Sprite(){ //函数内容部设置属性 this.name='shimily'; } //原型上设置方法 Sprite.prototype.show=function ...
- F2工作流引擎之-纯JS Web在线可拖拽的流程设计器(八)
Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回.传阅.转交,都可以非常方便快捷地实现,管理员 ...
- PDF 补丁丁 0.5.0.2713 发布(替换字库功能修正字符宽度问题)
新版本替换字库后,采用新字库的字符宽度.基本上可以满足一般的字库替换需求.请下载新版本测试.
- jquery 巧用json传参
JavaScript代码,巧用JSON传参数function AddComment(content) { var comment = {}; comment.threadId = $("#s ...
- -bash: msgunfmt: command not found
执行命令:msgunfmt frontend.mo -o frontend.po 解决方法: 安装gettext完成后如下:
- “iTunes无法连接iPad,因为设备超时”解决办法
注意一般要两个授权才会连接成功,一是在电脑上的iTunes登陆apple账户,账户授权电脑:二是ipad上信任Trust电脑连接ipad,如果没有重启iPad试试. 法1. 更新iTunes,重启电脑 ...
- onethinkp导入excel
/** * Excel导入函数 * @author crx349 */ if (!empty($_FILES)) { $config = array( 'maxSize' => 3145728, ...
- Python array,list,dataframe索引切片操作 2016年07月19日——智浪文档
array,list,dataframe索引切片操作 2016年07月19日——智浪文档 list,一维,二维array,datafrme,loc.iloc.ix的简单探讨 Numpy数组的索引和切片 ...
- Websocket 协议解析
WebSocket protocol 是HTML5一种新的协议.它是实现了浏览器与服务器全双工通信(full-duplex). 现 很多网站为了实现即时通讯,所用的技术都是轮询(po ...
- asp.net 5 中应用程序根目录及物理文件根目录的获取方式 此文已过期,不再适应rc1以后的版本
之前看了asp.net5,小弟就试着用了用,做了个小网站练习一下,有一个小模块需要上传图片到wwwroot下的images文件夹,但是aspnet5 发生了翻天复地变化,之前获取网站根目录的的方法不再 ...