iOS:城市级联列表的使用
1、介绍:
现在越来越多的项目都用到了地址,尤其是电商O2O的购物平台,我之前做的教育产品和电商产品都用到了,而实现地址的设置用到的技术就是城市级联列表,即普遍的做法就是自定义选择器控件UIPickerView,然后从本地整理的城市plist文件获取省份、城市、县镇三级级联。现在整理一下,分享给大家。
2、效果图如下:
3、用法:
(1)导入本地城市级联文件:provincecity.plist.zip
(2)导入DDMeZonePickerView.h头文件,在控制器通过[UIView Animation]的动画控制创建的DDMeZonePickerView视图对象的frame让其显示或隐藏即可,被选择的城市然后通过block进行回传。
4、废话不多说,实现代码如下:
DDMeZonePickerView.h
// DDMeZonePickerView.h
// BiaoJiePay
//
// Created by 夏远全 on 16/11/19.
// Copyright © 2016年 广州东德科技. All rights reserved.
// #import <UIKit/UIKit.h> /**
* 声明block
*/
typedef void (^adressBlock)(NSString *adress); @interface DDMeZonePickerView : UIView
/**
* 定义block属性
*/
@property (copy,nonatomic)adressBlock adressBlock;
/**
* 传递地址
*/
-(void)chooseAdressBlock:(adressBlock)adressBlock; @end
DDMeZonePickerView.m
//
// DDMeZonePickerView.m
// BiaoJiePay
//
// Created by 夏远全 on 16/11/19.
// Copyright © 2016年 广州东德科技. All rights reserved.
// #import "DDMeZonePickerView.h" @interface DDMeZonePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate> /** picker选择器*/
@property (strong, nonatomic)UIPickerView *customPicker;
/**
* 原始省份数组
*/
@property (strong, nonatomic)NSArray *provinceArray;
/**
* 原始城市数组
*/
@property (strong, nonatomic)NSArray *cityArray;
/**
* 原始区镇数组
*/
@property (strong, nonatomic)NSArray *townArray;
/**
* 选择省份字典
*/
@property (strong, nonatomic)NSDictionary *selectedProvinceDic;
/**
* 选择城市字典
*/
@property (strong, nonatomic)NSDictionary *selectedCityDic;
/**
* 确认
*/
@property (strong,nonatomic)UIButton *sureButton;
/**
* 取消
*/
@property (strong,nonatomic)UIButton *cancelButton; @end @implementation DDMeZonePickerView
/**
* 懒加载
*/
-(UIPickerView *)customPicker{
if (!_customPicker) {
_customPicker = [[UIPickerView alloc]init];
_customPicker.dataSource = self;
_customPicker.delegate = self;
_customPicker.backgroundColor = XYQColor(, , );
}
return _customPicker;
}
-(NSArray *)provinceArray{
if (!_provinceArray) {
_provinceArray = [NSArray array];
}
return _provinceArray;
}
-(NSArray *)cityArray{
if (!_cityArray) {
_cityArray = [NSArray array];
}
return _cityArray;
}
-(NSArray *)townArray{
if (!_townArray) {
_townArray = [NSArray array];
}
return _townArray;
}
-(NSDictionary *)selectedProvinceDic{
if (!_selectedProvinceDic) {
_selectedProvinceDic = [NSDictionary dictionary];
}
return _selectedProvinceDic;
}
-(NSDictionary *)selectedCityDic{
if (!_selectedCityDic) {
_selectedCityDic = [NSDictionary dictionary];
}
return _selectedCityDic;
} /**
* 初始化
*/
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self getPickerData];
self.customPicker.frame = CGRectMake(, , SCREEN_WIDTH, frame.size.height);
[self addSubview:self.customPicker];
}
return self;
} /**
* 从本地文件获取数据
*/
- (void)getPickerData{ NSString *path = [[NSBundle mainBundle] pathForResource:@"provincecity" ofType:@"plist"];
self.provinceArray = [NSArray arrayWithContentsOfFile:path]; //所有的省份 self.selectedProvinceDic = self.provinceArray[];
self.cityArray = [self.selectedProvinceDic objectForKey:@"city"];//所有的城市 self.selectedCityDic = self.cityArray[];
self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
} #pragma mark UIPickerViewDataSource
//设置有多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return ;
} //设置每列多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == ) {
return self.provinceArray.count;
} else if (component == ) {
return self.cityArray.count;
} else {
return self.townArray.count;
}
} #pragma mark - UIPickerViewDelegate
//自定义字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{ UILabel *myView = nil;
if (component == ) {
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, , )] ;
myView.text = [[self.provinceArray objectAtIndex:row] objectForKey:@"name"]; }else if(component == ){
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-)/, )];
myView.text = [[self.cityArray objectAtIndex:row] objectForKey:@"name"]; }else{
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-)/-, )];
myView.text = [[self.townArray objectAtIndex:row] objectForKey:@"name"];
}
myView.textAlignment = NSTextAlignmentCenter;
myView.font = [UIFont systemFontOfSize:]; //用label来设置字体大小
myView.backgroundColor = [UIColor whiteColor]; return myView;
} //设置宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return SCREEN_WIDTH/;
} //返回行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return ;
} //通过代理方法didSelectRows获取数据
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == ) { //获取省份选中行
self.selectedProvinceDic = self.provinceArray[row];
self.cityArray = [self.selectedProvinceDic objectForKey:@"city"];//所有的城市
self.selectedCityDic = self.cityArray[];
self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
[pickerView reloadComponent:];
[pickerView reloadComponent:]; }
if (component == ) { //获取城市选中行
NSInteger cityselectedRow = [pickerView selectedRowInComponent:];
self.selectedCityDic = self.cityArray[cityselectedRow];
self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
[pickerView reloadComponent:];
} //获取选中的省
NSString *provice = [NSString stringWithFormat:@"%@",[[self.provinceArray objectAtIndex:[pickerView selectedRowInComponent:]] objectForKey:@"name"]];
//获取选中的市
NSString *city = [NSString stringWithFormat:@"%@",[[self.cityArray objectAtIndex:[pickerView selectedRowInComponent:]] objectForKey:@"name"]];
//获取选中的县
NSString *town = [NSString stringWithFormat:@"%@",[[self.townArray objectAtIndex:[pickerView selectedRowInComponent:]] objectForKey:@"name"]]; //赋值
NSString *adressText = [NSString stringWithFormat:@"%@%@%@",provice,city,town];
self.adressBlock(adressText);
} /**
* 地址传值
*/
-(void)chooseAdressBlock:(void (^)(NSString *))adressBlock{
if (adressBlock) {
self.adressBlock = [adressBlock copy];
}
}@end
在ViewController.h懒加载选择器调用block取值:
/**
* 选择器
*/
-(DDMeZonePickerView *)zonePickerView{
if (!_zonePickerView) {
_zonePickerView = [[DDMeZonePickerView alloc]initWithFrame:CGRectMake(, SCREEN_HEIGHT, SCREEN_WIDTH, )];
[_zonePickerView chooseAdressBlock:^(NSString *adress) {
self.updateField.text = adress;
}];
}
return _zonePickerView;
}
本人原创,欢迎大家分享,转载需注明出处
iOS:城市级联列表的使用的更多相关文章
- MVVM架构~knockoutjs系列之包括区域级联列表的增删改
返回目录 这个例子我做了几次,之前总是有BUG,目前测试后,确定没有BUG才发上来的,主要功能是实现“我的银行”模块的增删改的功能,这个里面包括了级联列表的区域选择,这部分是难点,在开发过程中,我们应 ...
- 天气预报API(一):全国城市代码列表(“旧编码”)
说明 2016-12-09 补充 (后来)偶然发现中国天气网已经有城市ID列表的网页... 还发现城市编码有两种,暂且称中国天气网这些编码为旧标准 "旧编码"的特征是 9个字符长度 ...
- iOS 6分享列表——UIActivityViewController详解
iOS 6分享列表——UIActivityViewController详解 2013-06-03 01:42:33 发表评论 在iOS 6之后提供了一个分享列表视图,它通过UIActivity ...
- 使用NPOI生成Excel级联列表
目录 1 概要 1 2 磨刀不误砍柴工——先学会Excel中的操作 2 3 利用NPOI生成导入模板 7 3.1 设置workbook&sheet ...
- iOS TableView多级列表
代码地址如下:http://www.demodashi.com/demo/15006.html 效果预览 ### 一.需求 TableView多级列表:分级展开或合并,逐级获取并展示其子级数据,可以设 ...
- iOS系统声音列表
iOS系统声音列表 效果 说明 1. 点击cell就能发出声音 2. 只需要给出声音编号,就可以,非常简单易用 源码 https://github.com/YouXianMing/iOS-Utilit ...
- 气象城市ID列表
气象城市ID列表 数据来源: http://cj.weather.com.cn/support/Detail.aspx?id=51837fba1b35fe0f8411b6df 记录了2574个地区,2 ...
- 获取IOS应用安装列表
原文转载至 http://blog.csdn.net/justinjing0612/article/details/8887747 转自鸟哥博客:http://blog.cnrainbird.com/ ...
- GeneXus笔记本—城市级联下拉
最近在交流GeneXus的时候 总是会遇到有城市级联下拉的问题 这里就简单做几种方式 供大家参考参考 第一种就是直接绑定关联信息然后在后者的条件模块设定条件即可 具体如下: 首先我们所需要的表为pro ...
随机推荐
- CentOS6.5的openssl升级
CentOS6.5的openssl升级:(修复心脏漏血漏洞) [root@linux1 ~]# rpm -qi openssl|grep VersionVersion : 1.0.1e Vendor: ...
- [转载] #define new DEBUG_NEW
在用vc时,利用AppWizard会产生如下代码: #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] ...
- ITIL图示
- OpenCV学习笔记——图像的腐蚀与膨胀
顺便又复习了一下cvcopy如何进行图像拼接(最近觉得打开多幅图像分别看不如缩小掉放拼接到一幅图像上对比来的好) 首先把拼接的目标图像设置兴趣区域ROI,比如我有一个total,要把a.b.c分别从左 ...
- Ubuntu 安装字体
将字体Copy到/usr/share/fonts/windows目录下,然后执行以下几条命令更新系统字体缓存: cd /usr/share/fonts/vista/ sudo mkfontscale ...
- Super不要在Super构造器中调用覆盖方法
import java.util.Date; public class Super{ public Super(){ System."); overrideMe(); System.&quo ...
- SQL Server游标【转】
什么是游标 结果集,结果集就是select查询之后返回的所有行数据的集合. 游标则是处理结果集的一种机制吧,它可以定位到结果集中的某一行,多数据进行读写,也可以移动游标定位到你所需要的行中进行操作 ...
- 使用AJAX做关键字查询:输入框变化自动搜索、无刷新页面;
使用AJAX做关键字查询要求:1.无刷新页面2.输入框变化自动搜索 <style type="text/css"> .k{ width:150px; height:30 ...
- CLR调试报错“Visual Studio远程调试监视器 (MSVSMON.EXE) 的 64 位版本无法调试 32 位进程或 32 位转储。请改用 32 位版本”的解决
Win7 64位电脑上进行visual studio的数据库项目的CLR存储过程进行调试时,报错: ---------------------------Microsoft Visual Studio ...
- 蓝牙的SDP协议总结
1.概念 SDP协议让客户机的应用程序发现存在的服务器应用程序提供的服务以及这些服务的属性.SDP只提供发现服务的机制,不提供使用这些服务的方法.每个蓝牙设备都需要一个SDP Service, ...