【移动应用开发技术】怎么在iOS中实现一个三级联动选择器_第1页
【移动应用开发技术】怎么在iOS中实现一个三级联动选择器_第2页
【移动应用开发技术】怎么在iOS中实现一个三级联动选择器_第3页
【移动应用开发技术】怎么在iOS中实现一个三级联动选择器_第4页
【移动应用开发技术】怎么在iOS中实现一个三级联动选择器_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么在iOS中实现一个三级联动选择器

本篇文章为大家展示了怎么在iOS中实现一个三级联动选择器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1)怎么设置默认值,关键代码[self.pickerView

selectRow:xxx

inComponent:xxx

animated:YES];2)怎么让三级之间联动,关键代码[self

pickerView:self.pickerView

didSelectRow:0

inComponent:0

];//联动轮子1

必须得本轮有数据后触发否则crash先看下效果图关于设置默认值,三级联动,用UIPickView的话就是有3个轮子(component),首先我们要想到,第一次向后台发起请求,我们只能获取到第0个component的数据,只有当你滚动轮子的时候才会获取到省的Id发起请求来获得该省的市的数据,也就是第1个component的数据,依此类推,滚动第1个component发起请求来获取第2个component的数据,因此,pickView的监听轮子滚动的代理起了重要作用-

(void)pickerView:(UIPickerView

*)pickerView

didSelectRow:(NSInteger)row

inComponent:(NSInteger)component;我们通过接口获取第0个component的数据,这边是后台规定的使用id=0,获取到以后,默认选中第0个component的第0个row并主动调用触发pick的轮子滚动代理来联动第1个component【要在获取数据成功后再执行这部操作,因此放在数据请求成功的回调内】,代码为

[self

pickerView:self.pickerView

didSelectRow:0

inComponent:0

];在各轮子滚动过程中,用一个中间值_selectedRow0记录下第0个component的选中行_selectedRow1记录下第1个component的选中行_selectedRow2记录下第2个component的选中行,这里需要记住,滚动某个轮子只能对它后面的轮子产生影响,所以当滚动第0个component的时候使_selectedRow1,_selectedRow2均置为0,这里注意,上面提到的默认选中第0个component的第0个row并主动调用触发pick的轮子滚动代理来联动第1个component要先将轮子上的数据渲染好,设置好默认值才能主动调用监听轮子滚动的代理,否则会导致崩溃,另一个防崩溃的点如下图发现第三级没数据的时候,如果你在代码里没加【安全措施】,那也会导致崩溃,要在请求到第三级的数据后做下判断,如果个数为空,将该级对应的数据源置为nil。(其它两级的轮子最好也加判断)最后,由于这是个封装的类,最终要得到选中的详细信息,可通过代理或block传值给controller。又是你们最喜欢showcode环节:.h文件#import

<UIKit/UIKit.h>

//定制代理协议

@protocol

ZLMAddressPickerViewDelegate

<NSObject>

-

(void)addressPickerViewDidSelected:(NSString

*)areaName;//点击上方完成按钮的代理传回拼接好的选中地址

-

(void)addressPickerViewDidClose;//点击关闭代理

@end

@interface

ZLMAddressPickerView

:

UIView

@property

(weak,

nonatomic)

id<ZLMAddressPickerViewDelegate>

delegate;

@end.m文件#import

"ZLMAddressPickerView.h"

#import

"AFHttpUtils.h"

#import

"AreaModel.h"

@interface

ZLMAddressPickerView

()

<UIPickerViewDataSource,

UIPickerViewDelegate>

@property

(strong,

nonatomic)

UIPickerView

*pickerView;

@property

(strong,

nonatomic)

AreaModel

*provBridge;

@property

(strong,

nonatomic)

AreaModel

*cityBridge;

@property

(strong,

nonatomic)

AreaModel

*areaBridge;

@property

(copy,

nonatomic)

NSArray<Area

*>

*

provDataArr;//省数组

@property

(copy,

nonatomic)

NSArray<Area

*>

*

cityDataArr;//市数组

@property

(copy,

nonatomic)

NSArray<Area

*>

*

areaDataArr;//区数组

@end

@implementation

ZLMAddressPickerView

{

NSInteger

_selectRow0;//记录第0个轮子的选择行

NSInteger

_selectRow1;

NSInteger

_selectRow2;

NSString

*_areaString;//最后要传回的详细地址拼接字符串

Area

*_proModel;//记录下选中省的数据

Area

*_cityModel;

Area

*_areaModel;

}

-

(instancetype)initWithFrame:(CGRect)frame

{

self

=

[super

initWithFrame:frame];

if

(self)

{

[self

setup];

}

return

self;

}

-

(void)setup

{

_selectRow0

=

0;

_selectRow1

=

0;

_selectRow2

=

0;

self.backgroundColor

=

[UIColor

whiteColor];

UIToolbar

*toolbar

=

[[UIToolbar

alloc]

initWithFrame:CGRectMake(0,

0,

CGRectGetWidth(self.bounds),

44)];

toolbar.backgroundColor

=

[UIColor

whiteColor];

[self

addSubview:toolbar];

UIBarButtonItem

*closeItem

=

[[UIBarButtonItem

alloc]

initWithTitle:@"关闭"

style:UIBarButtonItemStylePlain

target:self

action:@selector(selectAddressClose)];

UIBarButtonItem

*completeItem

=

[[UIBarButtonItem

alloc]

initWithTitle:@"完成"

style:UIBarButtonItemStylePlain

target:self

action:@selector(selectAddressComplete)];

UIBarButtonItem

*spaceItem

=

[[UIBarButtonItem

alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:nil

action:nil];

toolbar.items

=

@[closeItem,

spaceItem,

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

{

NSMutableDictionary

*dic

=

[NSMutableDictionary

dictionaryWithDictionary:

@{@"id":@(0)}

];

[AFHttpUtils

sendPostTaskWithUrl:[NSString

stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL]

paramenters:dic

successHandle:^(NSURLSessionDataTask

*task,

id

responseObject)

{

NSLog(@"PROV:%@",responseObject);

vBridge

=

[AreaModel

mj_objectWithKeyValues:responseObject];

if

(vBridge.error_code==0)

{

vDataArr=vBridge.data;

[self

pickerView:self.pickerView

didSelectRow:0

inComponent:0

];//联动轮子1

必须得本轮有数据后才能触发didselect

[self.pickerView

reloadAllComponents];

}

}

errorHandle:^(NSError

*error)

{

}];

}

/*市*/

-

(void)downloadCityWithId:(NSInteger)provId

{

NSMutableDictionary

*dic

=

[NSMutableDictionary

dictionaryWithDictionary:

@{@"id":@(provId)}

];

[AFHttpUtils

sendPostTaskWithUrl:[NSString

stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL]

paramenters:dic

successHandle:^(NSURLSessionDataTask

*task,

id

responseObject)

{

NSLog(@"CITY:%@",responseObject);

self.cityBridge

=

[AreaModel

mj_objectWithKeyValues:responseObject];

if

(self.cityBridge.error_code==0)

{

self.cityDataArr=self.cityBridge.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.area_id];

}

}

errorHandle:^(NSError

*error)

{

}];

}

/*区*/

-

(void)downloadAreaWithId:(NSInteger)cityId

{

NSMutableDictionary

*dic

=

[NSMutableDictionary

dictionaryWithDictionary:

@{@"id":@(cityId)}

];

[AFHttpUtils

sendPostTaskWithUrl:[NSString

stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL]

paramenters:dic

successHandle:^(NSURLSessionDataTask

*task,

id

responseObject)

{

NSLog(@"AREA:%@",responseObject);

self.areaBridge

=

[AreaModel

mj_objectWithKeyValues:responseObject];

if

(self.areaBridge.error_code==0&&self.areaBridge.data.count>0)

{

self.areaDataArr=self.areaBridge.data;

}else{

self.areaDataArr=nil;

}

[self.pickerView

reloadComponent:2];

[self.pickerView

selectRow:0

inComponent:2

animated:YES];

[self

pickerView:self.pickerView

didSelectRow:0

inComponent:2

];

}

errorHandle:^(NSError

*error)

{

}];

}

#pragma

mark

-

events

response

-

(void)selectAddressComplete

{

[self.delegate

addressPickerViewDidSelected:_areaString];

}

-

(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

vDataArr.count;

}else

if(component==1){

return

self.cityDataArr.count;

}else{

return

self.areaDataArr.count;

}

}

-

(CGFloat)pickerView:(UIPickerView

*)pickerView

rowHeightForComponent:(NSInteger)component{

return

36;

}

//确定每个轮子的每一项显示什么内容

-

(NSAttributedString

*)pickerView:(UIPickerView

*)pickerView

attributedTitleForRow:(NSInteger)row

forComponent:(NSInteger)component{

NSDictionary

*

attrDic

=

@{NSForegroundColorAttributeName:[UIColor

blackColor],

NSFontAttributeName:[UIFont

systemFontOfSize:12]};

Area

*area;

if

(component==0)

{

area

=

vDataArr[row];

}else

if(component==1){

area

=

self.cityDataArr[row];

}else{

area

=

self.areaDataArr[row];

}

NSAttributedString

*

attrString

=

[[NSAttributedString

alloc]

initWithString:

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

=

vDataArr[_selectRow0];

[self

downloadCityWithId:_proModel.area_id];

}else

if(component==1){

_selectRow1

=

[pickerView

selectedRowInComponent:1];

_selectRow2

=

0;

_cityModel

=

self.cityDataArr[_selectRow1];

[self

downloadAreaWithId:_cityModel.area_id];

}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:@"%@

%@",_proM,_cityM];

}else{

_areaString

=

[NSString

stringWithFormat:@"%@

%@

%@",_proM,_cityM,_areaM];

}

}

#pragma

mark

-

getters

and

setters

-

(UIPickerView

*)pickerView

{

if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论