IOS第11天(3:UIPickerView省市联动)
*********
#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省市联动)的更多相关文章
- IOS第11天(2:UIPickerView自定义国旗选择)
国旗选择 #import "HMViewController.h" #import "HMFlag.h" #import "HMFlagView.h& ...
- IOS第11天(1:UIPickerView点餐)
UIPickerView #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSo ...
- IOS TableView实现省市联动
之前用UIPickerView实现了省市联动,上个月网友让用UITableView给他实现了下.今天也把这些贴出来. // // ViewController.m // doubleTable // ...
- jquery省市联动,根据公司需求而写
//author:guan //2015-05-25 //省市联动 //实用说明,页面引用如下js //<script src="../js/jquery-1.6.3.min.js&q ...
- jquery插件-省市联动
由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个. 首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下 ...
- select省市联动选择城市 asp.net mvc4
本文在 http://www.cnblogs.com/darrenji/p/3606703.html(感谢博主的分享)基础上加入全国各省市,从文件中读取全国省市县,组成省市联动的选择标签 在Model ...
- 省市联动 纯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 ...
- js实现的省市联动
最近工作,要用到省市联动的功能.网上搜了一下,发现有很多这样的例子,看了不少实例,把觉得写得不错的代码穿上来,好给大家分享一下. <!DOCTYPE html PUBLIC "-//W ...
- php省市联动实现
设计模式:ajax实现,数据库格式:id,name,parent_id 数据库: CREATE TABLE IF NOT EXISTS `city` ( `id` ) NOT NULL AUTO_IN ...
随机推荐
- Jenkins基础 - 常用配置操作
1.修改jenkins的根目录,默认地在C:\Users\用户名\.jenkins下(win7) 或C:\Documents and Settings\用户名\.jenkins(xp) 修改步骤: 增 ...
- JSON字符串和对象之间的转换
JSON(JavaScript Object Notation) 是JavaScript编程语言的一个子集.正因JSON是JavaScript的一个子集,所以它可清晰的运用于此语言中. eval函数 ...
- 每天一个linux命令---kill
linux中终止进程的命令--kill 一般用的是: 搜索pid: ps -ef|grep calendar 杀死pid:kill -9 pid 格式是:kill[参数][进程号]
- C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, ...
- ccc tiledmap 获取元素属性
cc.Class({ extends: cc.Component, properties: { elementLable: { default: null, type : cc.Label }, ma ...
- 纯CSS打造银色MacBook Air(完整版)
上一篇:<selection伪元素小解> ::selection{ background:blue; color:red; }p{font-size:14px;} 作者主页:myvin 博 ...
- JDBC 数据库异常 Exception 关闭的(语句,连接,ResultSet)
如果在rs.next()之前关闭了Statement或PreparedStatement,会导致下面的异常: java.sql.SQLException: 关闭的语句: next 如果在rs.next ...
- 移动端HTML5开发心得(转)
1. iOS里fixed中有input或者textarea,用户在里面输入文字,触发键盘,fixed容器会居中显示,而不是继续悬浮 解决办法: http://dwz.cn/CrwNz 2. ...
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- NOIP 2012 Day2T2 借教室题解
NOIP 2012 Day2T2 借教室题解 题目传送门:http://codevs.cn/problem/1217/ 题目描述 Description 在大学期间,经常需要租借教室.大到院系举办活动 ...