iOS之UUIUIPickview (二)
来源:互联网 发布:免费考勤软件 编辑:程序博客网 时间:2024/06/07 08:29
=======省市二级联动=====
#import <Foundation/Foundation.h>
@interface CZProvince : NSObject
/** 省的名称 */
@property (nonatomic,copy) NSString *name;
/** 对应的城市数据 */
@property (nonatomic,strong) NSArray *cities;
+ (instancetype)provinceWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
#import "CZProvince.h"
@implementation CZProvince
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [superinit];
if (self) {
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
+ (instancetype)provinceWithDict:(NSDictionary *)dict {
return [[selfalloc] initWithDict:dict];
}
**************************#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
*****************
#import "ViewController.h"
#import "CZProvince.h"
@interface ViewController () <UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak,nonatomic) IBOutletUIPickerView *pickerView;
@property (weak,nonatomic) IBOutletUILabel *provinceLbl;
@property (weak,nonatomic) IBOutletUILabel *cityLbl;
// 省模型的数组
@property (nonatomic,strong) NSArray *provinces;
// 保存上一次显示或选中的省
@property (nonatomic,strong) CZProvince *selPro;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 设置数据源.代理对象
self.pickerView.dataSource =self;
self.pickerView.delegate =self;
// 设置默认的选中
[selfpickerView:self.pickerViewdidSelectRow:0 inComponent:0];
}
#pragma mark - 代理方法
//在label上显示
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// 如果滚动的是第0列,要刷新第1列城市数据
if (component == 0) {
[pickerView reloadComponent:1];
// 选中第1列第0行
[pickerView selectRow:0inComponent:1 animated:YES];
}
// 获取省的名称获取市的名称
// 得获取pickerView第0列的选中行第1列的选中行
NSInteger selProIdx = [pickerViewselectedRowInComponent:0];
NSInteger selCityIdx = [pickerViewselectedRowInComponent:1];
// 设置省
CZProvince *selPro =self.provinces[selProIdx];
self.provinceLbl.text = selPro.name;
// 设置市
// self.cityLbl.text = selPro.cities[selCityIdx];
self.cityLbl.text =self.selPro.cities[selCityIdx];//注意用保存的省得模型去赋值
}
// 返回显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog(@"titleForRow");
// 如果是第0列,直接返回省的名称
if (component == 0) {
CZProvince *pro =self.provinces[row];
return pro.name;
} else {
// // 如果是第1列,根据省确定市的数据
// NSInteger selProIdx = [pickerView selectedRowInComponent:0];
// CZProvince *selPro = self.provinces[selProIdx];//获取省得模型
//
// // 城市的数组
// NSArray *cities = selPro.cities;
//
// // 返回内容
// return cities[row];
returnself.selPro.cities[row];
}
}
#pragma mark - 数据源方法
// 返回组的数量
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
// 返回每一组有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// 如果是第0列,行数就是模型数量
if (component == 0) {
returnself.provinces.count;
} else {
// 如果是第1列,根据第0列的省确定城市的数量
// 1. 获取第0列选中下标
NSInteger selProIdx = [pickerViewselectedRowInComponent:0];
// 2. 根据下标去获取省模型
CZProvince *selPro =self.provinces[selProIdx];
// 保存选中的省模型
self.selPro = selPro;
returnself.selPro.cities.count;
// // 3. 到模型中获取城市数量
// return selPro.cities.count;
}
}
#pragma mark - 懒加载
- (NSArray *)provinces {
if (_provinces ==nil) {
// 获取文件路径转为字典数组
NSArray *dictArr = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"02cities.plist"ofType:nil]];
// 遍历数组转为模型数组
// for (<#initialization#>; <#condition#>; <#increment#>) {
// <#statements#>
// }
// for (<#type *object#> in <#collection#>) {
// <#statements#>
// }
// for (id obj in dictArr) {
// NSDictionary *dict = (NSDictionary *)obj;
// CZProvince *pro = [CZProvince provinceWithDict:dict];
// }
// 通过block遍历
// 临时数组保存所有的模型
NSMutableArray *tempArrM = [NSMutableArrayarrayWithCapacity:dictArr.count];
[dictArr enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj,NSUInteger idx, BOOL *_Nonnull stop) {
CZProvince *province = [CZProvinceprovinceWithDict:obj];
[tempArrM addObject:province];
}];
_provinces = tempArrM;
}
return_provinces;
}
=========省市区三级联动========
//解析plist文件
-(void)parsePlistFile{
NSString *path=[[NSBundlemainBundle]pathForResource:@"area.plist"ofType:nil];
NSArray *arr=[NSArrayarrayWithContentsOfFile:path];
NSLog(@"plist---arr---%@",arr);
NSArray *modalArr=[LYHomeProvinceModalobjectArrayWithFilename:@"area.plist"];//字典转模型
self.provinceArr=modalArr;
}
//确定省市区编码
-(void)surePronvinceAndCityAndZone{
[selfparsePlistFile];//解析plist文件
UIView *bgv=[[UIViewalloc]initWithFrame:CGRectMake(0,HEIGHT/2, WIDTH, HEIGHT/2)];
self.bgv=bgv;
bgv.backgroundColor=[UIColorcolorWithWhite:0.9 alpha:1];
[self.viewaddSubview:bgv];
UIPickerView *pickview=[[UIPickerViewalloc]initWithFrame:CGRectMake(0, 30,WIDTH, HEIGHT/2-30)];
pickview.backgroundColor=[UIColorcolorWithWhite:0.9 alpha:1];
[bgv addSubview:pickview];
pickview.delegate=self;
pickview.dataSource=self;
self.pickview=pickview;
UIButton *cancelbtn=[[UIButtonalloc]initWithFrame:CGRectMake(WIDTH-140, 0, 60, 30)];
[cancelbtn setTitleColor:[UIColorcolorWithRed:39.0/255 green:131.0/255 blue:207.0/255alpha:1] forState:UIControlStateNormal];
[cancelbtn setTitle:@"取消"forState:UIControlStateNormal];
[bgv addSubview:cancelbtn];
[cancelbtn addTarget:selfaction:@selector(cancelClick)forControlEvents:UIControlEventTouchDown];
UIButton *surebtn=[[UIButtonalloc]initWithFrame:CGRectMake(WIDTH-70, 0, 60, 30)];
[surebtn setTitle:@"确定"forState:UIControlStateNormal];
[bgv addSubview:surebtn];
[surebtn setTitleColor:[UIColorcolorWithRed:39.0/255 green:131.0/255 blue:207.0/255alpha:1] forState:UIControlStateNormal];
[surebtn addTarget:selfaction:@selector(sureClick)forControlEvents:UIControlEventTouchDown];
// 设置默认的选中
[selfpickerView:self.pickviewdidSelectRow:0 inComponent:0];
}
-(void)cancelClick{
NSLog(@"点击执行");
CGRect frame=self.bgv.frame;
frame.origin.y=HEIGHT;
self.bgv.frame=frame;
self.bgv.alpha=0;
}
-(void)sureClick{
CGRect frame=self.bgv.frame;
frame.origin.y=HEIGHT;
self.bgv.frame=frame;
self.bgv.alpha=0;
}
#pragma mark - 代理方法
//选中一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(component==0){
NSInteger provincenum = [pickerViewselectedRowInComponent:0];//第1列选中的行--省
LYHomeProvinceModal *provincemodal =self.provinceArr[provincenum];
self.provinceModal=provincemodal;
self.provinceStr=provincemodal.state;
// 如果滚动的是第0列,要刷新第1列城市数据
[pickerView reloadComponent:1];
// 选中第1列第0行
[pickerView selectRow:0inComponent:1 animated:YES];
LYHomeCityModal *citymodal=provincemodal.cities[0];//每次刚启动时选择了省之后,市默认是0行
self.cityStr=citymodal.city;
if(citymodal.areas.count>0){
self.zoneStr=citymodal.areas[0];
}else {
self.zoneStr=@"";
}
if(self.provinceModal.cities[0]){//选择省的时候刷新完市,再刷新区
// 如果滚动的是第1列,要刷新第2列城市数据
[pickerView reloadComponent:2];
// 选中第2列第0行
[pickerView selectRow:0inComponent:2 animated:YES];
}
}elseif(component==1){
LYHomeProvinceModal *provincemodal=self.provinceModal;
NSInteger citynum = [pickerViewselectedRowInComponent:1];//第2列选中的行--市
LYHomeCityModal *citymodal=provincemodal.cities[citynum];
self.cityStr=citymodal.city;
if(citymodal.areas.count>0){
self.zoneStr=citymodal.areas[0];//选择了市之后区默认选择的是0行;
}elseif(citymodal.areas.count==0){
self.zoneStr=@"";
}
// 如果滚动的是第1列,要刷新第2列城市数据
[pickerView reloadComponent:2];
// 选中第2列第0行
[pickerView selectRow:0inComponent:2 animated:YES];
}elseif(component==2){
NSInteger zonenum = [pickerViewselectedRowInComponent:2];//第3列选中的行--区
LYHomeCityModal *citymodal=self.citymodal;
if(citymodal.areas.count==0){
self.zoneStr=@"";
}else {
self.zoneStr=citymodal.areas[zonenum];
}
}
self.provinceAndCityAndZone(self.provinceStr,self.cityStr,self.zoneStr);//block调用传值
}
//// 返回每一行显示的内容
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// if(component==0){
// LYHomeProvinceModal *provinceModal=self.provinceArr[row];
// return provinceModal.state;
//
// }else if(component==1){
// LYHomeProvinceModal *provinceModal=self.provinceModal;
// LYHomeCityModal *citymodal=provinceModal.cities[row];
// return citymodal.city;
//
// }else if(component==2){
//
// LYHomeCityModal *citymodal=self.citymodal;
// return citymodal.areas[row];
// }
// return @"安徽";
//}
//-自定义的显示,可以自己设置字体大小字体颜色
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *v=[[UIViewalloc]initWithFrame:CGRectMake(0, 0,WIDTH/3-20,30)];
UILabel *lbl=[[UILabelalloc]initWithFrame:CGRectMake(20, 0,WIDTH/3-20,30)];
[v addSubview:lbl];
lbl.font=[UIFontsystemFontOfSize:15];
if(component==0){
LYHomeProvinceModal *provinceModal=self.provinceArr[row];
lbl.text=provinceModal.state;
}elseif(component==1){
LYHomeProvinceModal *provinceModal=self.provinceModal;
LYHomeCityModal *citymodal=provinceModal.cities[row];
lbl.text=citymodal.city;
}elseif(component==2){
LYHomeCityModal *citymodal=self.citymodal;
lbl.text=citymodal.areas[row];
}
return v;
}
#pragma mark - 数据源方法
// 返回有多少列"组"
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
// 返回每一组有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(component==0){//省
returnself.provinceArr.count;
}elseif(component==1){//市
NSInteger provincenum = [pickerViewselectedRowInComponent:0];//第1列选中的行
// 2. 根据下标去获取省模型
LYHomeProvinceModal *provinceModal =self.provinceArr[provincenum];
self.provinceModal=provinceModal;//保存选中的省模型
return provinceModal.cities.count;
}elseif(component==2){//区
NSInteger citynum = [pickerViewselectedRowInComponent:1];//第2列选中的行
NSLog(@"--区---%tu",citynum);
LYHomeProvinceModal *provinceModal=self.provinceModal;
LYHomeCityModal *citymodal=provinceModal.cities[citynum];//选中的市模型
self.citymodal=citymodal;//保存市模型
NSArray *zoneArr=citymodal.areas;
NSLog(@"--zonearr--%tu",zoneArr.count);
return zoneArr.count;
}
// 返回行数
return 10;
}
- iOS之UUIUIPickview (二)
- IOS之开发资料二
- IOS教材单词之二
- iOS多线程之二 NSOperation
- iOS SDk开发之二
- IOS 学习之二指针
- IOS之Objective-C学习笔记(二)
- IOS设计模式之二:Delegate模式
- IOS设计模式之二:Delegate模式
- IOS设计模式之二:Delegate模式
- IOS之正则表达式(二)
- ios学习笔记(二)之协议
- IOS设计模式之二:Delegate模式
- IOS设计模式之二:Delegate模式
- iOS开发之二:UIWindow与UIView
- iOS 基础小结 常用控件之二
- IOS算法(二)之选择排序
- IOS多线程之——GCD(二)
- 【Spring】Spring Framework Reference Documentation中文版22
- 快速排序
- Python模拟百度自动输入搜索功能
- Apache Beam开发指南
- BZOJ 2763: [JLOI2011]飞行路线 分层图最短路
- iOS之UUIUIPickview (二)
- FFMPEG学习【libavformat】:实用函数
- Python设置浏览器宽高
- 将DOS批处理文件转换为Shell脚本
- java中this关键字的使用(code)
- PHP使用strtotime获取上个月、下个月、本月的日期
- Beam从零开始(一)
- 刷清橙OJ--A1072.潜伏者
- Z-STACK学习笔记-OSAL分析