ios收货地址三级联动选择
Posted 回忆12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios收货地址三级联动选择相关的知识,希望对你有一定的参考价值。
这次的需求,是省市区三级联动选择,并且市的选择要根据省的id,然后区的选择要根据市的id
每次都要进行网络请求,关键代码,其实是
//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
然后我就上传我的代码,这个代码我也是根据别人的修改的,因为需求不一样
1,创建一个View
//
// IWAreaPickerView.h
// InternetBank
//
// Created by zmit on 2017/9/8.
// Copyright ? 2017年 CreditCloud. All rights reserved.
//
#import <UIKit/UIKit.h>
//定制代理协议
@protocol IWAreaPickerViewDelegate <NSObject>
- (void)addressPickerViewDidSelected:(NSString *)areaName withDic:(NSDictionary *)info;//点击上方完成按钮的代理传回拼接好的选中地址
- (void)addressPickerViewDidClose;//点击关闭代理
@end
@interface IWAreaPickerView : UIView
@property (weak, nonatomic) id<IWAreaPickerViewDelegate> delegate;
@end
//
// IWAreaPickerView.m
// InternetBank
//
// Created by zmit on 2017/9/8.
// Copyright ? 2017年 CreditCloud. All rights reserved.
//
#import "IWAreaPickerView.h"
#import "ProvincesModel.h" //省的json类型
#import "CitysModel.h" //市的json类型
#import "AreasModel.h" // 区的json类型
#import "APIClient.h"
static const NSInteger KProvinceComponent = 0;
static const NSInteger KCityComponent = 1;
static const NSInteger KDistrictComponent = 2;
#define KFont14 [UIFont systemFontOfSize:14]
@interface IWAreaPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) ProvinceModel *provBridge;
@property (strong, nonatomic) CityModel *cityBridge;
@property (strong, nonatomic) AreaModel *areaBridge;
@property (strong, nonatomic) NSMutableArray *provDataArr;//省数组
@property (strong, nonatomic) NSMutableArray *cityDataArr;//市数组
@property (strong, nonatomic) NSMutableArray * areaDataArr;//区数组
@end
@implementation IWAreaPickerView
{
NSInteger _selectRow0;//记录第0个轮子的选择行
NSInteger _selectRow1;
NSInteger _selectRow2;
NSString *_areaString;//最后要传回的详细地址拼接字符串
NSDictionary *_infoDic;
ProvinceModel *_proModel;//记录下选中省的数据
CityModel *_cityModel;
AreaModel *_areaModel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
self.provDataArr = [[NSMutableArray alloc]init];
self.cityDataArr = [[NSMutableArray alloc]init];
self.areaDataArr = [[NSMutableArray alloc]init];
}
return self;
}
- (void)setup {
_selectRow0 = 0;
_selectRow1 = 0;
_selectRow2 = 0;
self.backgroundColor = [UIColor colorWithRed:222/255.0 green:227/255.0 blue:237/255.0 alpha:1.0];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 44)];
toolbar.backgroundColor = [UIColor clearColor];
[self addSubview:toolbar];
UIBarButtonItem *closeItem = [[ UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_x"] style:UIBarButtonItemStylePlain target:self action:@selector(selectAddressClose)];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithTitle:@"选择所在地区" style:UIBarButtonItemStylePlain target:self action:@selector(selectAddressClose)];
UIBarButtonItem *spaceItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *completeItem = [[ UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_right"] style:UIBarButtonItemStylePlain target:self action:@selector(selectAddressComplete)];
toolbar.items = @[closeItem, spaceItem,titleItem,spaceItem2, completeItem];
self.pickerView.frame = CGRectMake(0, 44, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - 44);
[self addSubview:self.pickerView];
[self downloadProv];
}
#pragma mark - http methods
/*省*/
- (void)downloadProv
{
NSString *url = [[APIClient instance]apiEnjoyAddressOfGetProvice:@"ios" withPromotion:@"0000"];
[self.provDataArr removeAllObjects];
[[APIClient instance]getGoodsProvince:url success:^(ProvincesModel *result)
{
if (result)
{
[self.provDataArr addObjectsFromArray:result.data];
[self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];//联动轮子1 必须得本轮有数据后才能触发didselect
[self.pickerView reloadAllComponents];
}
} failure:^(NSString *error) {
}];
}
/*市*/
- (void)downloadCityWithId:(NSString *)provId
{
NSString *url = [[APIClient instance]apiEnjoyAddressOfGetCitiesByProvinceCode:provId withDeviceType:@"IOS" withPromotion:@"0000"];
[self.cityDataArr removeAllObjects];
[[APIClient instance]getGoodsCity:url success:^(CitysModel *result)
{
if (result)
{
[self.cityDataArr addObjectsFromArray:result.data];
[self.pickerView reloadComponent:1];
[self.pickerView selectRow:0 inComponent:1 animated:YES];//默认选择row0
[self pickerView:self.pickerView didSelectRow:0 inComponent:1 ];//联动轮子2 必须得本轮有数据后才能触发didselect
_cityModel = self.cityDataArr[_selectRow1];
[self downloadAreaWithId:_cityModel.cityCode];
}
} failure:^(NSString *error) {
}];
}
/*区*/
- (void)downloadAreaWithId:(NSString *)cityId
{
NSString *url = [[APIClient instance]apiEnjoyAddressOfGetAreaByCityCode:cityId withDeviceType:@"IOS" withPromotion:@"0000"];
[self.areaDataArr removeAllObjects];
[[APIClient instance]getGoodsArea:url success:^(AreasModel *result)
{
if (result)
{
[self.areaDataArr addObjectsFromArray:result.data];
[self.pickerView reloadComponent:2];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
[self pickerView:self.pickerView didSelectRow:0 inComponent:2 ];
}
} failure:^(NSString *error) {
}];
}
#pragma mark - events response
- (void)selectAddressComplete {
[self.delegate addressPickerViewDidSelected:_areaString withDic:_infoDic];
}
- (void)selectAddressClose {
[self.delegate addressPickerViewDidClose];
}
#pragma mark - UIPickerViewDataSource
//确定picker的轮子个数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
//确定picker的每个轮子的item数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component==0) {
return self.provDataArr.count;
}else if(component==1){
return self.cityDataArr.count;
}else{
return self.areaDataArr.count;
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 40;
}
//确定每个轮子的每一项显示什么内容
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSDictionary * attrDic = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:6]};
ProvinceModel *provice = [[ProvinceModel alloc]init];
CityModel *city = [[CityModel alloc]init];
AreaModel *area = [[AreaModel alloc]init];
NSAttributedString * attrString;
if (component==0) {
provice = (ProvinceModel *)self.provDataArr[row];
attrString = [[NSAttributedString alloc] initWithString:provice.provinceName attributes:attrDic];
}else if(component==1){
city = (CityModel *)self.cityDataArr[row];
attrString = [[NSAttributedString alloc] initWithString:city.cityName attributes:attrDic];
}else{
area = (AreaModel *)self.areaDataArr[row];
attrString = [[NSAttributedString alloc] initWithString:area.areaName attributes:attrDic];
}
return attrString;
}
//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component==0) {
_selectRow0 = [pickerView selectedRowInComponent:0];
_selectRow1 = 0;
_selectRow2 = 0;
_proModel = self.provDataArr[_selectRow0];
[self downloadCityWithId:_proModel.provinceCode];
}else if(component==1){
_selectRow1 = [pickerView selectedRowInComponent:1];
_selectRow2 = 0;
_cityModel = self.cityDataArr[_selectRow1];
[self downloadAreaWithId:_cityModel.cityCode];
}else{
_selectRow2 = [pickerView selectedRowInComponent:2];
if (self.areaDataArr&&self.areaDataArr.count>0) {
_areaModel = self.areaDataArr[_selectRow2];
}else{
_areaModel = nil;
}
}
if(_areaModel==nil){
_areaString = [NSString stringWithFormat:@"%@%@",_proModel.provinceName,_cityModel.cityName];
if (_proModel && _cityModel)
{
_infoDic = @{
@"proID":_proModel.provinceCode,
@"cityId":_cityModel.cityCode,
};
}
NSLog(@"_info=%@",_infoDic);
}else{
_areaString = [NSString stringWithFormat:@"%@%@%@",_proModel.provinceName,_cityModel.cityName,_areaModel.areaName];
if (_proModel && _cityModel && _areaModel)
{
_infoDic = @{
@"proID":_proModel.provinceCode,
@"cityId":_cityModel.cityCode,
@"areaId":_areaModel.areaCode,
};
}
NSLog(@"_info=%@",_infoDic);
}
}
//修改字体样式
- (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 = 60;
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, myViewW, myViewH)];
myView.font = KFont14;
myView.backgroundColor = [UIColor clearColor];
myView.textAlignment = NSTextAlignmentCenter;
if (component == KProvinceComponent) {
self.provBridge = [_provDataArr objectAtIndex:row];
myView.text = self.provBridge.provinceName;
}
else if (component == KCityComponent) {
self.cityBridge = [_cityDataArr objectAtIndex:row];
myView.text = self.cityBridge.cityName;
}
else {
self.areaBridge = [_areaDataArr objectAtIndex:row];
myView.text = self.areaBridge.areaName;
}
return myView;
}
#pragma mark - getters and setters
- (UIPickerView *)pickerView {
if (_pickerView == nil) {
_pickerView = [[UIPickerView alloc] init];
_pickerView.delegate = self;
_pickerView.dataSource = self;
}
return _pickerView;
}
@end
VC中的实现
{ NSLog(@"选择地址");
[[UIApplication sharedApplication].keyWindow addSubview:self.maskView];
_areaPickerView = [[IWAreaPickerView alloc] initWithFrame:CGRectMake(0, kDeviceHeight - 350, self.view.frame.size.width, 350)];
_areaPickerView.delegate = self;
[self.maskView addSubview:_areaPickerView];
}
/***********************代理的实现***********************/
#pragma mark - IWAreaPickerViewDelegate
- (void)addressPickerViewDidSelected:(NSString *)areaName withDic:(NSDictionary *)info{
addressInfo = [NSDictionary dictionaryWithDictionary:info];
NSLog(@"areaName = %@",areaName);
addAddressView.nFlag = @"have";
addAddressView.addressInfo = areaName;
[self addressPickerViewDidClose];
}
以上是关于ios收货地址三级联动选择的主要内容,如果未能解决你的问题,请参考以下文章