*********

#import "ViewController.h"
#import "Province.h" @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> @property (nonatomic,strong)NSArray *provinces; @property (nonatomic,assign)NSInteger indexOfProvice;//当前默认选中的省份
@end @implementation ViewController -(NSArray *)provinces{
if (!_provinces) {
_provinces = [Province provinceList];
} return _provinces;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%@",self.provinces);
} // returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return ;
} // returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == ) {//省分
return self.provinces.count;
} //获取对应省份的城市个数
Province *province = self.provinces[self.indexOfProvice]; return province.cities.count; } #pragma mark 显示数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == ){//显示省份的名字
//对应列行的省份
Province *province = self.provinces[row];
return province.name;
} //获取选中的城市,显示城市名字
Province *selectedProvice = self.provinces[self.indexOfProvice];
return selectedProvice.cities[row];
} //-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//
// UILabel *label = (UILabel *)view;
// if (!label) {
// label = [[UILabel alloc] init];
// }
//
// if(component == 0){//显示省份的名字
// //对应列行的省份
// Province *province = self.provinces[row];
// label.text = province.name;
// label.backgroundColor = [UIColor grayColor];
// }else{
//
// //获取选中的城市,显示城市名字
// Province *selectedProvice = self.provinces[self.indexOfProvice];
// label.text = selectedProvice.cities[row];
// label.backgroundColor = [UIColor blueColor];
// }
//
//
//
//
// return label;
//} #pragma mark 选中行 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == ) {//第一列省的选中改变后,就要更新第二列数据
//更新选中省份的索引
self.indexOfProvice = row; //刷新数据
//全部刷新
//[pickerView reloadAllComponents]; //部份刷新
[pickerView reloadComponent:]; //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行
[pickerView selectRow: inComponent: animated:YES];
}
} #pragma mark 设置宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component == ) {
return ;
} return ;
} @end

***model.m

#import "Province.h"

@implementation Province

-(instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
} return self;
}
+(instancetype)provinceWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
} +(NSArray *)provinceList{
//plist文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
NSArray *provincePlist = [NSArray arrayWithContentsOfFile:filePath]; NSMutableArray *provinceM = [NSMutableArray array];
for (NSDictionary *dic in provincePlist) {
Province *prov = [Province provinceWithDict:dic];
[provinceM addObject:prov];
} return provinceM; }
@end

****model.h

#import <UIKit/UIKit.h>

@interface Province : NSObject

@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)NSArray *cities; -(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)provinceWithDict:(NSDictionary *)dict; +(NSArray *)provinceList; @end

IOS第11天(3:UIPickerView省市联动)的更多相关文章

  1. IOS第11天(2:UIPickerView自定义国旗选择)

    国旗选择 #import "HMViewController.h" #import "HMFlag.h" #import "HMFlagView.h& ...

  2. IOS第11天(1:UIPickerView点餐)

    UIPickerView #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSo ...

  3. IOS TableView实现省市联动

    之前用UIPickerView实现了省市联动,上个月网友让用UITableView给他实现了下.今天也把这些贴出来. // // ViewController.m // doubleTable // ...

  4. jquery省市联动,根据公司需求而写

    //author:guan //2015-05-25 //省市联动 //实用说明,页面引用如下js //<script src="../js/jquery-1.6.3.min.js&q ...

  5. jquery插件-省市联动

        由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个.     首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下 ...

  6. select省市联动选择城市 asp.net mvc4

    本文在 http://www.cnblogs.com/darrenji/p/3606703.html(感谢博主的分享)基础上加入全国各省市,从文件中读取全国省市县,组成省市联动的选择标签 在Model ...

  7. 省市联动 纯html+js

    在js里面声明所有数据,并根据html的select事件触发js实现填充对应的数据到下拉框. 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  8. js实现的省市联动

    最近工作,要用到省市联动的功能.网上搜了一下,发现有很多这样的例子,看了不少实例,把觉得写得不错的代码穿上来,好给大家分享一下. <!DOCTYPE html PUBLIC "-//W ...

  9. php省市联动实现

    设计模式:ajax实现,数据库格式:id,name,parent_id 数据库: CREATE TABLE IF NOT EXISTS `city` ( `id` ) NOT NULL AUTO_IN ...

随机推荐

  1. express随记01

    系统变量的设置 app.get(env) | process.env.NODE_ENV: 会自动判断当前环境类型; app.get(port) | process.env.PORT: 必须手动设置; ...

  2. express-5 质量保证(2)

    跨页测试 跨页测试更有挑战性,因为需要你控制和观测浏览器. 现在设置一个跨页测试情境的例子.比如,你的网站上有一个包含联系表单的Request Group Rate页面.营销部门想知道客户是从哪个页面 ...

  3. JS实现有点炫的图片展示效果-图片解体和组合

    经过4个月的努力学习,迎来了进入市场的最后一个学习项目.自己模仿了一个图片展示效果,用在了项目中,感觉挺炫的.在这里分享一下,希望大家喜欢~! bomb-showImg : 在线演示http://ru ...

  4. 一个java集合使用bug

    在使用java集合的时候有的时候集合是来自于一些第三方工具提供的从字符串或json 转出集合的对象有时是抽象类,这时的对象部分功能是未实现的,在使用相应操作的时侯 会引发bug. Exception  ...

  5. 原生 js 左右切换轮播图

    使用方法: 可能很多人对轮播图感兴趣,下面奉上本人的 原生 js 轮播代码复制 js 到页面的最底部,样式在 css 里改,js 基本不用动,有什么不懂的可以 加本人 QQ172360937 咨询 或 ...

  6. 如何在UMG中上显示3D物体

    http://www.unrealchina.net/forum.php?mod=viewthread&tid=101548 个人认为可以使用关卡系统,搞个空关卡来做渲染比较好

  7. ASP.NET Core EF Sample

    Install EF Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityF ...

  8. 【原】iOS学习41之多线程

    1. 多线程概述 1> 程序.进程和进程的概念 程序:由源代码生成的可执行应用.(例如:QQ.app) 进程:一个正在运行的程序可以看做一个进程.(例如:正在运行的QQ就是一个进程),进程拥有独 ...

  9. Hadoop Streaming框架使用(一)

      Streaming简介 link:http://www.cnblogs.com/luchen927/archive/2012/01/16/2323448.html Streaming框架允许任何程 ...

  10. (转)hbase master挂掉-zookeeper连接超时原因

    link:http://www.51testing.com/?uid-445759-action-viewspace-itemid-812467 并行运行hbase删表,建表操作,多个表多个regio ...