博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
省市选择
阅读量:4595 次
发布时间:2019-06-09

本文共 2582 字,大约阅读时间需要 8 分钟。

#import "ViewController.h"

 

#import "CZCityModel.h"

 

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *myPickerVeiw;

@property (weak, nonatomic) IBOutlet UILabel *lbProvince;

@property (weak, nonatomic) IBOutlet UILabel *lbCity;

 

//创建数据集合

@property(nonatomic,strong)NSArray *cityArray;

 

 

//临时集合

@property(nonatomic,strong)NSArray *tmpArray;

 

@end

 

 

 

@implementation ViewController

 

 

#pragma mark - 懒加载

- (NSArray *)cityArray

{

    if (!_cityArray) {

        

        NSString *path = [[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil];

        

        NSArray *cities = [NSArray arrayWithContentsOfFile:path];

        

        NSMutableArray *tmpArray = [NSMutableArray array];

        

        

        for (NSDictionary *dict in cities) {

            

            CZCityModel *model = [CZCityModel cityWithDict:dict];

            

        

            [tmpArray addObject:model];

            

        }

        

        _cityArray = tmpArray;

    }

 

    return _cityArray;

}

 

 

#pragma mark - pickerView的数据源方法

//返回多少列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 2;

}

 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

//    return [self.cityArray[component] count];

    

    //判断是不是第1列

    

    if (component == 0) {//省

        

        return self.cityArray.count;

        

    }else{//市

    

        //1.获取当前行 0列的行数

        NSInteger currentRow = [pickerView selectedRowInComponent:0];

        

    

        //2.是不是根据当前行 获取对应得城市列表

        CZCityModel *model = self.cityArray[currentRow];

        

        

        self.tmpArray = model.cities;

        

        return model.cities.count;

    }

    

}

 

#pragma mark - pickerView 的代理方法

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    

//    @(row) 几个意思??? NSNumber ;所有基本数据类型都是转换为NSNumber;

    NSLog(@"titleForRow ----%@---",@(row));

    

    //判断 当前的列

    if(component == 0){//省

        

        //省的名字

        CZCityModel *model = self.cityArray[row];

        

        

        return model.name;

        

    

    }else{

        

        //1.获取你选中的是当前的哪个的省份

//        NSInteger currentRow = [pickerView selectedRowInComponent:0];

//        

//        //2.是不是根据选取的是哪个省 来获取对应的城市模型

//        CZCityModel *model = self.cityArray[currentRow];

//

//        //3.取出对应的城市

//        return model.cities[row];

        

        return self.tmpArray[row];

        

 

    }

}

 

 

//停止的时候调用

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    

    

    //1.刷新

    if (component == 0) {//省列

        

        //1.刷新城市

        //全局

//        [pickerView reloadAllComponents];

        

        //局部

        [pickerView reloadComponent:1];

        

        //2. 自动选中 1列的第0行

        [pickerView selectRow:0 inComponent:1 animated:YES];

        

    }

    

    //2.赋值 注意点: 你观察下  需要临时集合的帮助吗>??

    

    

    

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

   

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

转载于:https://www.cnblogs.com/pals/p/5094958.html

你可能感兴趣的文章
LeetCode【6】. ZigZag Conversion --思路图解与java实现
查看>>
git 合并分支
查看>>
NSNotification与NSNotificationCenter
查看>>
qt 中文乱码 处理QByteArray类型里含中文的数据
查看>>
跨库事务一致性问题的解决方式(例)
查看>>
ios build时,Undefined symbols for architecture xxx问题的总结
查看>>
JavaScript对象
查看>>
IIS7(Windows7)下最简单最强安装多版本PHP支持环境
查看>>
关于Cocos2d-x发布游戏的时候遇到的问题和解决
查看>>
CSS学习笔记之样式声明
查看>>
rtmpdump代码分析 转
查看>>
codeforces #322 div 2 D. Three Logos (枚举)
查看>>
20145202马超《JAVA》预备作业1
查看>>
[导入]参考OpenSceneGraph的3ds插件学习lib3ds
查看>>
java基础-四大特征
查看>>
linux文档查看器
查看>>
如何使用 ccs7.2调试代码
查看>>
2016.8.22 Axure两级下拉框联动的实现
查看>>
C#集合类:动态数组、队列、栈、哈希表、字典(转)
查看>>
基于bootstrap 的datatable插件的使用(php版)
查看>>