IOS 省市区三级联动

2024-05-31 14:48
文章标签 ios 联动 三级 省市区

本文主要是介绍IOS 省市区三级联动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:在大部分的商城类App里面都会有这样的一个功能,那就是添加收货地址,这时候难免就会涉及到添加省市区。

先来一张效果图


代码如下:

#import <UIKit/UIKit.h>typedef void(^IWAreaPickerViewConfirmBlock)(NSString *areaStr);
typedef void(^IWAreaPickerViewCancleBlock)();@interface IWAreaPickerView : UIView@property (nonatomic,copy) NSDictionary *areaDict;
//确认回调
@property (nonatomic,copy) IWAreaPickerViewConfirmBlock areaPickerViewConfirmBlock;
//失败回调
@property (nonatomic,copy) IWAreaPickerViewCancleBlock areaPickerViewCancleBlock;@end

#import "IWAreaPickerView.h"static const NSInteger KProvinceComponent = 0;
static const NSInteger KCityComponent = 1;
static const NSInteger KDistrictComponent = 2;#define KFont14 [UIFont systemFontOfSize:14]@interface IWAreaPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{UIPickerView    *_areapicker;NSArray         *_province;NSArray         *_city;NSArray         *_district;
}
@end@implementation IWAreaPickerView- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {//1.设置背景色self.backgroundColor = [UIColor whiteColor];self.layer.borderColor = [UIColor colorWithRed:202/255.0 green:202/255.0 blue:202/255.0 alpha:1.0].CGColor;self.layer.borderWidth = 0.5;//2.添加UIPickerView[self addPickerView];//3.添加数据NSBundle *bundle = [NSBundle mainBundle];NSString *plistPath = [bundle pathForResource:@"area" ofType:@"plist"];self.areaDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];}return self;
}#pragma mark 设置省市区的数据字典
- (void)setAreaDict:(NSDictionary *)areaDict
{_areaDict = areaDict;[self selectedProvinceIndex:0 cityIndex:0];[_areapicker reloadAllComponents];
}#pragma mark 获取对应的省市区数据
- (void)selectedProvinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex
{//取出省NSArray *components = [_areaDict allKeys];NSArray *sortedArray = [components sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {if ([obj1 integerValue] > [obj2 integerValue]) {return NSOrderedDescending;}if ([obj1 integerValue] < [obj2 integerValue]) {return NSOrderedAscending;}return NSOrderedSame;}];NSMutableArray *provinceTmp = [NSMutableArray array];for (int i = 0; i < sortedArray.count; i ++) {NSString *index = sortedArray[i];NSArray *tmp = [[_areaDict objectForKey:index] allKeys];[provinceTmp addObject:tmp[0]];}_province = [NSArray arrayWithArray:provinceTmp];//取出市NSString *index = sortedArray[provinceIndex];NSString *selected = _province[provinceIndex];NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[[_areaDict objectForKey:index] objectForKey:selected]];NSArray *cityComponents = [dic allKeys];NSArray *citySortedArray = [cityComponents sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {if ([obj1 integerValue] > [obj2 integerValue]) {return NSOrderedDescending;}if ([obj1 integerValue] < [obj2 integerValue]) {return NSOrderedAscending;}return NSOrderedSame;}];NSMutableArray *cityTmp = [NSMutableArray array];for (int i = 0; i < citySortedArray.count; i ++) {NSString *index = citySortedArray[i];NSArray *tmp = [[dic objectForKey:index] allKeys];[cityTmp addObject:tmp[0]];}_city = [NSArray arrayWithArray:cityTmp];//取出区NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary:[dic objectForKey:citySortedArray[cityIndex]]];NSArray *array = [[NSArray alloc] initWithArray:[cityDic allKeys]];NSString *selectedCity = array[0];_district = [[NSArray alloc] initWithArray:cityDic[selectedCity]];
}#pragma mark 添加UIPickerView
- (void)addPickerView
{CGFloat topViewH = self.frame.size.height * 0.2;UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, topViewH)];topView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;[self addSubview:topView];UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];cancleBtn.titleLabel.font = KFont14;[cancleBtn setTitleColor:[UIColor colorWithRed:124/255.0 green:128/255.0 blue:249/255.0 alpha:1.0] forState:UIControlStateNormal];[cancleBtn setTitle:@"取消" forState:UIControlStateNormal];CGFloat BtnY = 0;CGFloat BtnH = topView.frame.size.height;CGFloat BtnW = BtnH*1.5;cancleBtn.frame = CGRectMake(0, BtnY, BtnW, BtnH);[cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];[topView addSubview:cancleBtn];UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];confirmBtn.titleLabel.font = cancleBtn.titleLabel.font;[confirmBtn setTitle:@"确定" forState:UIControlStateNormal];[confirmBtn setTitleColor:[UIColor colorWithRed:124/255.0 green:128/255.0 blue:249/255.0 alpha:1.0] forState:UIControlStateNormal];CGFloat confirmBtnX = self.frame.size.width - BtnW;confirmBtn.frame = CGRectMake(confirmBtnX, BtnY, BtnW, BtnH);[confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];[self addSubview:confirmBtn];UIPickerView *picker = [[UIPickerView alloc] init];picker.frame = CGRectMake(0, CGRectGetMaxY(topView.frame), self.frame.size.width, self.frame.size.height - CGRectGetMaxY(topView.frame));picker.backgroundColor = [UIColor colorWithRed:213/255.0 green:216/255.0 blue:223/255.0 alpha:1.0f];picker.delegate = self;picker.dataSource = self;picker.showsSelectionIndicator = YES;[self addSubview:picker];_areapicker = picker;
}#pragma mark 取消
- (void)cancle
{if (_areaPickerViewCancleBlock) {_areaPickerViewCancleBlock();}
}#pragma mark 确定
- (void)confirm
{NSInteger provinceIndex = [_areapicker selectedRowInComponent: KProvinceComponent];NSInteger cityIndex = [_areapicker selectedRowInComponent: KCityComponent];NSInteger districtIndex = [_areapicker selectedRowInComponent: KDistrictComponent];NSString *provinceStr = [_province objectAtIndex: provinceIndex];NSString *cityStr = [_city objectAtIndex: cityIndex];NSString *districtStr = [_district objectAtIndex:districtIndex];if ([provinceStr isEqualToString:cityStr] && [cityStr isEqualToString:districtStr]) {cityStr = @"";districtStr = @"";}else if ([cityStr isEqualToString:districtStr]) {districtStr = @"";}NSString *showMsg = [NSString stringWithFormat:@"%@,%@,%@",provinceStr,cityStr,districtStr];if (_areaPickerViewConfirmBlock) {_areaPickerViewConfirmBlock(showMsg);}
}#pragma mark - UIPickerView数据源和代理
#pragma mark 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{return 3;
}#pragma mark 行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{if (component == KProvinceComponent) {return _province.count;}else if (component == KCityComponent) {return _city.count;}else {return _district.count;}
}#pragma mark 每一行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{if (component == KProvinceComponent) {return [_province objectAtIndex:row];}else if (component == KCityComponent){return [_city objectAtIndex:row];}else {return [_district objectAtIndex:row];}
}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{if (component == KProvinceComponent) {[self selectedProvinceIndex:row cityIndex:0];[_areapicker selectRow:0 inComponent:KCityComponent animated:YES];[_areapicker selectRow:0 inComponent:KDistrictComponent animated:YES];[_areapicker reloadComponent:KCityComponent];[_areapicker reloadComponent:KDistrictComponent];}else if (component == KCityComponent) {NSInteger provinceIndex = [_areapicker selectedRowInComponent: KProvinceComponent];[self selectedProvinceIndex:provinceIndex cityIndex:row];[_areapicker selectRow:0 inComponent:KDistrictComponent animated:YES];[_areapicker reloadComponent:KDistrictComponent];}
}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{UILabel *myView = nil;CGFloat myViewW = pickerView.frame.size.width *0.33;CGFloat myViewH = 30;myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, myViewW, myViewH)];myView.font = KFont14;myView.backgroundColor = [UIColor clearColor];myView.textAlignment = NSTextAlignmentCenter;if (component == KProvinceComponent) {myView.text = [_province objectAtIndex:row];}else if (component == KCityComponent) {myView.text = [_city objectAtIndex:row];}else {myView.text = [_district objectAtIndex:row];}return myView;
}@end


demo下载链接: http://download.csdn.net/detail/u011154007/9663322

这篇关于IOS 省市区三级联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018234

相关文章

iOS HTTPS证书不受信任解决办法

之前开发App的时候服务端使用的是自签名的证书,导致iOS开发过程中调用HTTPS接口时,证书不被信任 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAu

vue+elementUI下拉框联动显示

<el-row><el-col :span="12"><el-form-item label="主账号:" prop="partyAccountId" :rules="[ { required: true, message: '主账号不能为空'}]"><el-select v-model="detailForm.partyAccountId" filterable placeholder="

IOS 数组去重的几种方式

本来只知道NSSet和KeyValues的。今天又新学了几种方式 还有就是和同事学的一种方式 外层循环从0开始遍历,内层从最后一个元素开始遍历 for(int i=0;i<index;i++){  for(int j=index-1;j>i;j-- ){ } }

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]  2015-04-24 11:40  956人阅读  评论(0)  收藏  举报   分类:   iOS 基础篇(208)  版权声明:本文为博主原创文章,未经博主允许不得转载。 Assertion

iOS:编译时出现no such file or directory:xxx以及use twice...filenames are used to distinguish private dec

简    注册  登录   添加关注 作者  婉卿容若 2016.04.29 11:22 写了21870字,被16人关注,获得了14个喜欢 iOS:编译时出现"no such file or directory:xxx"以及"use twice...filenames are used to distinguish private

iOS 到处 ipa包的时候 会有四个选项分别代表什么

如图 在 iOS 到处 ipa包的时候 会有四个选项  1.Save for iOS App Store Deployment 保存到本地 准备上传App Store 或者在越狱的iOS设备上使用 2.Save for Ad Hoc Deployment 保存到本地 准备在账号添加的可使用设备上使用(具体为在开发者账户下添加可用设备的udid),该app包是发布证书编

iOS 7适配上存在的各种问题

谈谈项目中遇到的各种iOS7适配问题 由于我的项目要适配到iOS7.1, 而现在已经是9时代了,在实际工作中我也是遇到了各种奇葩的坑,所以我想尽快把遇到的iOS7适配问题和解决方案分享出来,以后这些东西可能就用处不大了。   1.字体问题 iOS7中的字体适配恐怕是最麻烦的坑了,原因是iOS7以上的许多字体在7都是不存在的,甚至包括一些system-字体。比如system-

人机的三级抽象

数学的三级抽象包括第一级抽象是数表示万物、第二级抽象是字母表征数、第三级抽象是运算规则的抽象(如群论),在人机交互中,类比于数学的三级抽象,可以理解为: 第一级抽象:用户界面和操作的抽象化。这一级别涉及到将用户与计算机之间的交互过程抽象化,使得用户可以通过直观的界面和操作来控制计算机或者访问信息。例如,图形用户界面 (GUI) 将复杂的计算机功能和操作抽象为图标、按钮和菜单,使用户能够轻松地进行

潜艇伟伟迷杂交版植物大战僵尸2024最新免费安卓+ios苹果+iPad分享

嗨,亲爱的游戏迷们!今天我要给你们种草一个超有趣的游戏——植物大战僵尸杂交版。这款游戏不仅继承了原有经典游戏的核心玩法,还加入了许多创新元素,让玩家能够体验到前所未有的乐趣。快来跟随我一起探索这个神奇的世界吧! 植物大战僵尸杂交版最新绿色版下载链接: https://pan.quark.cn/s/d60ed6e4791c 🔥 创新与经典的完美结合 植物大战僵尸杂交版在保持了原游戏经典玩

SQL是如何支持三级模式的

文章目录 三级模式的详细描述SQL 如何支持三级模式1. 支持内部模式2. 支持概念模式3. 支持外部模式 示例总结 问题讲解——SQL是如何支持三级模式的 在数据库系统中,三级模式(Three-Schema Architecture)是由ANSI/SPARC数据库管理系统标准委员会在1970年代提出的一个框架,用来描述数据库系统的体系结构。它包含三个不同的层次:内部模式