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年份, 最新数据, 长期更新, 开源维护, 支持 ...
随机推荐
- 基于bootstrap的后台二级垂直菜单[转]
最近做一个后台的管理项目,用到了Twitter推出的bootstrap前端开发工具包,是一个基于css3/html5的框架.花周末时间,写了一个非常简单后台的菜单.本着开源的精神,现在把它分享出来(呵 ...
- axis2开发webservice入门到精通
1,准备工作: 首先我们要下载:axis2-1.4.1-war(发布webservice),axis2-1.4.1-bin.zip(webservice调用使用的各种包). 下载好了,把axis2-1 ...
- MergeRecord_C++中map的使用
//============================================================================ // Name : TTTest.cpp ...
- Yii2.0 rules验证规则大全
required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...
- SQL基础语句(提升)
1.复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 2.拷贝表 insert into b(a,b,c) select d ...
- Cannot send session cache limiter Cannot modify header information
当php报出 Cannot send session cache limiter 或Cannot modify header information 的错误时 其理论上是因为php代码以前有 ...
- Java系列笔记(2) - Java RTTI和反射机制
目录 前言 传统的RTTI 反射 反射的实现方式 反射的性能 反射与设计模式 前言 并不是所有的Class都能在编译时明确,因此在某些情况下需要在运行时再发现和确定类型信息(比如:基于构建编程,),这 ...
- Spring Security (一)
一.pom.xml <!-- spring security --> <dependency> <groupId>org.springframework.secur ...
- DataStructure——红黑树学习笔记
1.前言 本文伪码和解释参考: http://blog.csdn.net/v_JULY_v/article/details/6105630 C实现的源码本文未贴出,请见: http://blog.cs ...
- 常用OpenLDAP命令
ldappasswd -x -D "cn=Manager,dc=clouderachina,dc=com" -W "uid=mis,ou=Group,dc=clouder ...