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 ...
随机推荐
- 宫格布局实例(注意jquery的版本号要统一)
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- 常用meta标签举例说明
本文是作者从百度百科和其他从网页中搜索到的资料,经综合整理,把常用meta标签列举并示例说明,仅供参考. 1.<meta charset="UTF-8"> --- ch ...
- Codeforces Round #375 (Div. 2) - C
题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...
- blade用法
一.blade条件判断,foreach循环写法 @if(isset($fileInfo) && !empty($fileInfo)) @foreach($fileInfo as $k) ...
- CentOS 6.6 下配置软RAID5
在虚拟机中增加4块5G的硬盘 安装依赖包 yum install -y parted mdadm 查看一下硬盘信息fdisk -l
- ACM 懒省事的小明
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...
- Redis_Jedis使用总结
目录:1.pipeline2.跨jvm的id生成器3.跨jvm的锁实现(watch multi)4.redis分布式 1. Pipeline 官方的说明是:starts a pipeline,whic ...
- Linux多线程实例练习 - pthread_create()
Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...
- PL/SQL无法连接,提示:pl/sql initialization error sql*net not properly installed
各种找资料都不行,最后还是在公司的线上环境中去找不同点配置好的.不多说了,就做了如下配置: 服务器环境是:windows server 2008 64位操作系统 1.电脑系统环境变量PATH增加:x: ...
- jsp实现过滤器的简单例子
web.xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&quo ...