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 ...
随机推荐
- AOP动态代理解析2-代码织入入口
通过自定义配置完成了对AnnotationAwareAspectJAutoProxyCreator类型的自动注册,那么这个类到底做了什么工作来完成AOP的操作呢?首先我们看看AnnotationAwa ...
- jQuery-表格以及表单
表单应用: 1.设置高度: $comment.height($comment.height() + 50); $comment.animate({height : "+=50"}, ...
- jdbc 各驱动写法
1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...
- 从Sql server 2008获取表字段属性信息,注释信息
select b.[value] from sys.columns a left join sys.extended_properties b on a.object_id=b.major_id ...
- Redis List命令
命令 解释 lpush key string 在key对应list的头部添加字符串元素,返回1表示成功,0表示key存在且不是list类型. rpush key string 同上,尾插入. ...
- 快学Java NIO 续篇
可以先看Java NIO的整体介绍,这篇接着说以下内容,<快学Java NIO>续篇 FileChannel SocketChannel ServerSocketChannel Java ...
- MongoDB 入门之基础 DML
此文章主要记录部分主要的 MongoDB Collection 的 DML 操作. 文章中的 Collection 名字为 yourColl,每一次操作包含以下两条初始数据 { "_id&q ...
- javaScript怪癖分析
最近了解到javascript中有些编程怪癖现象,很有意思,有必要总结一下: 1.未知变量名创建全局变量 在我们平常的编写javascript程序的时候,有的人写法不是很正规,在定义变量的时候 直接定 ...
- BZOJ3740 : pku2842 N-dimension Matching N维匹配
做$n$次递推求出$n$维Hash值即可,时间复杂度$O(nk)$. #include<cstdio> #define N 500010 int n,i,j,t,delta,ca,cb,a ...
- Mybatis Generator insert useGeneratedKeys keyProperty
Mybatis自动生成代码,需要用到mybatis Generator,详见http://mybatis.github.io/generator/configreference/generatedKe ...