Initial commit
This commit is contained in:
BIN
Binary file not shown.
Executable
+28
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// AreaModel.h
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface AreaModel : NSObject
|
||||
/**
|
||||
* GRADE
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *GRADE;
|
||||
/**
|
||||
* 区域ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *ID;
|
||||
/**
|
||||
* 区域名
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *NAME;
|
||||
/**
|
||||
* 区域的上一级ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *PARENT_AREA_ID;
|
||||
@end
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// AreaModel.m
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AreaModel.h"
|
||||
|
||||
@implementation AreaModel
|
||||
|
||||
@end
|
||||
Executable
+136
@@ -0,0 +1,136 @@
|
||||
//
|
||||
// ChinaArea.h
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "FMDB.h"
|
||||
#include "ProvinceModel.h" // 省份数据模型
|
||||
#import "CityModel.h" // 城市数据模型
|
||||
#import "AreaModel.h" // 区域数据模型
|
||||
|
||||
@interface ChinaArea : NSObject
|
||||
|
||||
|
||||
|
||||
// 公开省-市-区三个模型属性
|
||||
/**
|
||||
* 省份模型
|
||||
*/
|
||||
@property (nonatomic,strong)ProvinceModel *provinceModel;
|
||||
/**
|
||||
* 城市模型
|
||||
*/
|
||||
@property (nonatomic,strong)CityModel *cityModel;
|
||||
/**
|
||||
* 地区模型
|
||||
*/
|
||||
@property (nonatomic,strong)AreaModel *areaModel;
|
||||
|
||||
|
||||
/**
|
||||
* 插入省份数据
|
||||
*
|
||||
* @param province 省份模型
|
||||
*/
|
||||
- (void)insterProvince:(ProvinceModel *)province;
|
||||
/**
|
||||
* 插入城市数据
|
||||
*
|
||||
* @param city 城市模型
|
||||
*/
|
||||
- (void)insterCity:(CityModel *)city;
|
||||
/**
|
||||
* 插入区域数据
|
||||
*
|
||||
* @param area 区域模型
|
||||
*/
|
||||
- (void)insterArea:(AreaModel *)area;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有省份模型的集合数组
|
||||
*
|
||||
* @return 返回所有省份数据模型的集合
|
||||
*/
|
||||
- (NSMutableArray *)getAllProvinceData;
|
||||
/**
|
||||
* 根据省份ID获取对应的省份数据模型
|
||||
*
|
||||
* @param provinceID 省份ID
|
||||
*
|
||||
* @return 省份数据模型
|
||||
*/
|
||||
- (ProvinceModel *)getProvinceDataByID:(NSString *)provinceID;
|
||||
/**
|
||||
* 根据省份ID获取该省份的所有城市数据模型的集合
|
||||
*
|
||||
* @param parentID 省份ID
|
||||
*
|
||||
* @return 一个省份的城市数据模型集合
|
||||
*/
|
||||
- (NSMutableArray *)getCityDataByParentID:(NSString *)parentID;
|
||||
/**
|
||||
* 根据城市ID获取对应的城市数据模型
|
||||
*
|
||||
* @param cityID 城市ID
|
||||
*
|
||||
* @return 城市数据模型
|
||||
*/
|
||||
- (CityModel *)getCityDataByID:(NSString *)cityID;
|
||||
/**
|
||||
* 根据城市ID获取该城市的所有区域数据模型的集合
|
||||
*
|
||||
* @param parentID 城市ID
|
||||
*
|
||||
* @return 一个城市的区域数据模型集合
|
||||
*/
|
||||
- (NSMutableArray *)getAreaDataByParentID:(NSString *)parentID;
|
||||
/**
|
||||
* 根据地区ID获取对应的地区数据模型
|
||||
*
|
||||
* @param areaID 地区ID
|
||||
*
|
||||
* @return 地区数据模型
|
||||
*/
|
||||
- (AreaModel *)getAreaDataByID:(NSString *)areaID;
|
||||
/**
|
||||
* 制作省份数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 省份ID
|
||||
* @param NAME 省份名称
|
||||
* @param PARENT_AREA_ID 上一级ID
|
||||
*
|
||||
* @return 省份数据模型
|
||||
*/
|
||||
- (ProvinceModel *)makeProvinceModel:(NSNumber *)GRADE provinceID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID;
|
||||
/**
|
||||
* 制作城市数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 城市ID
|
||||
* @param NAME 城市名称
|
||||
* @param PARENT_AREA_ID 上一级省份ID
|
||||
*
|
||||
* @return 城市数据模型
|
||||
*/
|
||||
- (CityModel *)makeCityModel:(NSNumber *)GRADE cityID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID;
|
||||
/**
|
||||
* 制作区域数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 区域ID
|
||||
* @param NAME 区域名称
|
||||
* @param PARENT_AREA_ID 上一级城市D
|
||||
*
|
||||
* @return 区域数据模型
|
||||
*/
|
||||
- (AreaModel *)makeAreaModel:(NSNumber *)GRADE areaID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID;
|
||||
|
||||
@end
|
||||
Executable
+311
@@ -0,0 +1,311 @@
|
||||
//
|
||||
// ChinaArea.m
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ChinaArea.h"
|
||||
|
||||
@interface ChinaArea ()
|
||||
|
||||
@property (nonatomic,strong)FMDatabaseQueue *fmdbQueue;
|
||||
@property (nonatomic,strong)FMDatabase *dataBase;
|
||||
@property (nonatomic,copy)NSString *dbPath;
|
||||
@end
|
||||
|
||||
@implementation ChinaArea
|
||||
|
||||
- (instancetype)init{
|
||||
if (self = [super init]) {
|
||||
[self initDataBase];
|
||||
// [self creatProvinceTabel];
|
||||
// [self creatCityTabel];
|
||||
// [self creatAreaTabel];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化数据库
|
||||
*/
|
||||
- (void)initDataBase{
|
||||
self.dbPath=[[NSBundle mainBundle]pathForResource:@"china_citys_name.db" ofType:nil];
|
||||
self.fmdbQueue = [[FMDatabaseQueue alloc] initWithPath:self.dbPath];
|
||||
self.dataBase = [[FMDatabase alloc] initWithPath:self.dbPath];
|
||||
}
|
||||
/**
|
||||
* 创建省份表单
|
||||
*/
|
||||
- (void)creatProvinceTabel{
|
||||
[self.dataBase open];
|
||||
[self.dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS Province (rowid INTEGER PRIMARY KEY AUTOINCREMENT, GRADE text,ID text,NAME text,PARENT_AREA_ID text)"];
|
||||
[self.dataBase close];
|
||||
}
|
||||
/**
|
||||
* 创建城市表单
|
||||
*/
|
||||
- (void)creatCityTabel{
|
||||
[self.dataBase open];
|
||||
[self.dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS City (rowid INTEGER PRIMARY KEY AUTOINCREMENT, GRADE text,ID text,NAME text,PARENT_AREA_ID text)"];
|
||||
[self.dataBase close];
|
||||
}
|
||||
/**
|
||||
* 创建区域表单
|
||||
*/
|
||||
- (void)creatAreaTabel{
|
||||
[self.dataBase open];
|
||||
[self.dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS Area (rowid INTEGER PRIMARY KEY AUTOINCREMENT, GRADE text,ID text,NAME text,PARENT_AREA_ID text)"];
|
||||
[self.dataBase close];
|
||||
}
|
||||
/**
|
||||
* 插入省份数据
|
||||
*
|
||||
* @param province 省份模型
|
||||
*/
|
||||
- (void)insterProvince:(ProvinceModel *)province{
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[db open];
|
||||
[db executeUpdate:@"INSERT INTO Province (GRADE,ID,NAME,PARENT_AREA_ID) VALUES (?,?,?,?)",province.GRADE,province.ID,province.NAME,province.PARENT_AREA_ID];
|
||||
[db close];
|
||||
}];
|
||||
}
|
||||
/**
|
||||
* 插入城市数据
|
||||
*
|
||||
* @param city 城市模型
|
||||
*/
|
||||
- (void)insterCity:(CityModel *)city{
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[db open];
|
||||
[db executeUpdate:@"INSERT INTO City (GRADE,ID,NAME,PARENT_AREA_ID) VALUES (?,?,?,?)",city.GRADE,city.ID,city.NAME,city.PARENT_AREA_ID];
|
||||
[db close];
|
||||
}];
|
||||
}
|
||||
/**
|
||||
* 插入区域数据
|
||||
*
|
||||
* @param area 区域模型
|
||||
*/
|
||||
- (void)insterArea:(AreaModel *)area{
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[db open];
|
||||
[db executeUpdate:@"INSERT INTO Area (GRADE,ID,NAME,PARENT_AREA_ID) VALUES (?,?,?,?)",area.GRADE,area.ID,area.NAME,area.PARENT_AREA_ID];
|
||||
[db close];
|
||||
}];
|
||||
}
|
||||
/**
|
||||
* 获取所有省份模型的集合数组
|
||||
*
|
||||
* @return 返回所有省份数据模型的集合
|
||||
*/
|
||||
- (NSMutableArray *)getAllProvinceData{
|
||||
NSMutableArray *provinceArray = [[NSMutableArray alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM Province"];
|
||||
while ([result next]) {
|
||||
ProvinceModel *model = [[ProvinceModel alloc] init];
|
||||
model.GRADE = [result stringForColumn:@"GRADE"];
|
||||
model.ID = [result stringForColumn:@"ID"];
|
||||
model.NAME = [result stringForColumn:@"NAME"];
|
||||
model.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
[provinceArray addObject:model];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return provinceArray;
|
||||
}
|
||||
/**
|
||||
* 根据省份ID获取对应的省份数据模型
|
||||
*
|
||||
* @param provinceID 省份ID
|
||||
*
|
||||
* @return 省份数据模型
|
||||
*/
|
||||
- (ProvinceModel *)getProvinceDataByID:(NSString *)provinceID{
|
||||
ProvinceModel *pmodel = [[ProvinceModel alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM Province WHERE ID = ?",provinceID];
|
||||
while ([result next]) {
|
||||
pmodel.GRADE = [result stringForColumn:@"GRADE"];
|
||||
pmodel.ID = [result stringForColumn:@"ID"];
|
||||
pmodel.NAME = [result stringForColumn:@"NAME"];
|
||||
pmodel.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return pmodel;
|
||||
}
|
||||
/**
|
||||
* 根据省份ID获取该省份的所有城市数据模型的集合
|
||||
*
|
||||
* @param parentID 省份ID
|
||||
*
|
||||
* @return 一个省份的城市数据模型集合
|
||||
*/
|
||||
- (NSMutableArray *)getCityDataByParentID:(NSString *)parentID{
|
||||
NSMutableArray *cityArray = [[NSMutableArray alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM City WHERE PARENT_AREA_ID = ?",parentID];
|
||||
while ([result next]) {
|
||||
CityModel *model = [[CityModel alloc] init];
|
||||
model.GRADE = [result stringForColumn:@"GRADE"];
|
||||
model.ID = [result stringForColumn:@"ID"];
|
||||
model.NAME = [result stringForColumn:@"NAME"];
|
||||
model.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
[cityArray addObject:model];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return cityArray;
|
||||
}
|
||||
/**
|
||||
* 根据城市ID获取对应的城市数据模型
|
||||
*
|
||||
* @param cityID 城市ID
|
||||
*
|
||||
* @return 城市数据模型
|
||||
*/
|
||||
- (CityModel *)getCityDataByID:(NSString *)cityID{
|
||||
CityModel *pmodel = [[CityModel alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM City WHERE ID = ?",cityID];
|
||||
while ([result next]) {
|
||||
pmodel.GRADE = [result stringForColumn:@"GRADE"];
|
||||
pmodel.ID = [result stringForColumn:@"ID"];
|
||||
pmodel.NAME = [result stringForColumn:@"NAME"];
|
||||
pmodel.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return pmodel;
|
||||
}
|
||||
/**
|
||||
* 根据城市ID获取该城市的所有区域数据模型的集合
|
||||
*
|
||||
* @param parentID 城市ID
|
||||
*
|
||||
* @return 一个城市的区域数据模型集合
|
||||
*/
|
||||
- (NSMutableArray *)getAreaDataByParentID:(NSString *)parentID{
|
||||
NSMutableArray *areaArray = [[NSMutableArray alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM Area WHERE PARENT_AREA_ID = ?",parentID];
|
||||
while ([result next]) {
|
||||
AreaModel *model = [[AreaModel alloc] init];
|
||||
model.GRADE = [result stringForColumn:@"GRADE"];
|
||||
model.ID = [result stringForColumn:@"ID"];
|
||||
model.NAME = [result stringForColumn:@"NAME"];
|
||||
model.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
[areaArray addObject:model];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return areaArray;
|
||||
}
|
||||
/**
|
||||
* 根据地区ID获取对应的地区数据模型
|
||||
*
|
||||
* @param areaID 地区ID
|
||||
*
|
||||
* @return 地区数据模型
|
||||
*/
|
||||
- (AreaModel *)getAreaDataByID:(NSString *)areaID{
|
||||
AreaModel *pmodel = [[AreaModel alloc] init];
|
||||
[self.fmdbQueue inDatabase:^(FMDatabase *db) {
|
||||
[self openChinaAreaDB:db];
|
||||
FMResultSet *result = [db executeQuery:@"SELECT * FROM Area WHERE ID = ?",areaID];
|
||||
while ([result next]) {
|
||||
pmodel.GRADE = [result stringForColumn:@"GRADE"];
|
||||
pmodel.ID = [result stringForColumn:@"ID"];
|
||||
pmodel.NAME = [result stringForColumn:@"NAME"];
|
||||
pmodel.PARENT_AREA_ID = [result stringForColumn:@"PARENT_AREA_ID"];
|
||||
}
|
||||
[db close];
|
||||
}];
|
||||
return pmodel;
|
||||
}
|
||||
/**
|
||||
* 打开数据库
|
||||
*
|
||||
* @param db db description
|
||||
*/
|
||||
- (void)openChinaAreaDB:(FMDatabase *)db{
|
||||
BOOL isOpen = [db open];
|
||||
if (isOpen == YES) {
|
||||
// ITLog(@"数据库ChinaArea.db 打开成功!\n路径 = %@",self.dbPath);
|
||||
}
|
||||
else{
|
||||
// ITLog(@"数据库ChinaArea.db 打开失败!\n路径 = %@",self.dbPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 制作省份数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 省份ID
|
||||
* @param NAME 省份名称
|
||||
* @param PARENT_AREA_ID 上一级ID
|
||||
*
|
||||
* @return 省份数据模型
|
||||
*/
|
||||
- (ProvinceModel *)makeProvinceModel:(NSNumber *)GRADE provinceID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID{
|
||||
ProvinceModel *model = [[ProvinceModel alloc] init];
|
||||
model.GRADE = [NSString stringWithFormat:@"%@",GRADE];
|
||||
model.ID = [NSString stringWithFormat:@"%@",ID];
|
||||
model.NAME = NAME;
|
||||
model.PARENT_AREA_ID = [NSString stringWithFormat:@"%@",PARENT_AREA_ID];
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 制作城市数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 城市ID
|
||||
* @param NAME 城市名称
|
||||
* @param PARENT_AREA_ID 上一级省份ID
|
||||
*
|
||||
* @return 城市数据模型
|
||||
*/
|
||||
- (CityModel *)makeCityModel:(NSNumber *)GRADE cityID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID{
|
||||
CityModel *model = [[CityModel alloc] init];
|
||||
model.GRADE = [NSString stringWithFormat:@"%@",GRADE];
|
||||
model.ID = [NSString stringWithFormat:@"%@",ID];
|
||||
model.NAME = NAME;
|
||||
model.PARENT_AREA_ID = [NSString stringWithFormat:@"%@",PARENT_AREA_ID];
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 制作区域数据模型
|
||||
*
|
||||
* @param GRADE GRADE
|
||||
* @param ID 区域ID
|
||||
* @param NAME 区域名称
|
||||
* @param PARENT_AREA_ID 上一级城市D
|
||||
*
|
||||
* @return 区域数据模型
|
||||
*/
|
||||
- (AreaModel *)makeAreaModel:(NSNumber *)GRADE areaID:(NSNumber *)ID name:(NSString *)NAME parentId:(NSNumber *)PARENT_AREA_ID{
|
||||
AreaModel *model = [[AreaModel alloc] init];
|
||||
model.GRADE = [NSString stringWithFormat:@"%@",GRADE];
|
||||
model.ID = [NSString stringWithFormat:@"%@",ID];
|
||||
model.NAME = NAME;
|
||||
model.PARENT_AREA_ID = [NSString stringWithFormat:@"%@",PARENT_AREA_ID];
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@end
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// ChinaPlckerView.h
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "ChinaArea.h"
|
||||
|
||||
@protocol ChinaPlckerViewDelegate <NSObject>
|
||||
|
||||
@optional
|
||||
/**
|
||||
* 获取地址选择器的数据模型
|
||||
*
|
||||
* @param chinaModel ChinaArea数据模型
|
||||
*/
|
||||
- (void)chinaPlckerViewDelegateChinaModel:(ChinaArea *)chinaModel;
|
||||
/**
|
||||
* 收起选择器
|
||||
*/
|
||||
- (void)chinaPickerViewDelegateHidden;
|
||||
|
||||
@end
|
||||
|
||||
@interface ChinaPlckerView : UIView
|
||||
|
||||
@property (nonatomic,assign) id<ChinaPlckerViewDelegate> delegate;
|
||||
/**
|
||||
* 添加城市选择器
|
||||
*
|
||||
* @param delegate 代理
|
||||
* @param views 父视图
|
||||
*
|
||||
* @return 城市选择器
|
||||
*/
|
||||
+(ChinaPlckerView *)customChinaPicker:(id)delegate superView:(UIView*)views;
|
||||
|
||||
@end
|
||||
+393
@@ -0,0 +1,393 @@
|
||||
//
|
||||
// ChinaPlckerView.m
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ChinaPlckerView.h"
|
||||
|
||||
// 屏幕的高度
|
||||
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
|
||||
|
||||
// 屏幕的宽度
|
||||
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
|
||||
|
||||
//键盘高度 253
|
||||
#define IT_KEYBOARD_HEIGHT 253
|
||||
|
||||
#define ROW_HEIGHT 40
|
||||
|
||||
//设置RGB颜色值
|
||||
#define COLOR(R,G,B,A) [UIColor colorWithRed:(CGFloat)R/255.0 green:(CGFloat)G/255.0 blue:(CGFloat)B/255.0 alpha:A]
|
||||
|
||||
@interface ChinaPlckerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
|
||||
/**
|
||||
* 地址选择器
|
||||
*/
|
||||
@property (nonatomic,strong)UIPickerView *pickerView;
|
||||
/**
|
||||
* 确定按钮
|
||||
*/
|
||||
@property (nonatomic,strong)UIButton *sureButton;
|
||||
/**
|
||||
* 蒙蔽曾
|
||||
*/
|
||||
@property (nonatomic,strong)UIView *hideView;
|
||||
/**
|
||||
* 选择器视图
|
||||
*/
|
||||
@property (nonatomic,strong)UIView *addPickVview;
|
||||
/**
|
||||
* 数据模型
|
||||
*/
|
||||
@property (nonatomic,strong)ChinaArea *chinaModel;
|
||||
/**
|
||||
* 选择的省份ID
|
||||
*/
|
||||
@property (nonatomic,strong)NSString *selectedProvinceID;
|
||||
/**
|
||||
* 选择的城市ID
|
||||
*/
|
||||
@property (nonatomic,strong)NSString *selectedCityID;
|
||||
/**
|
||||
* 选择的区域ID
|
||||
*/
|
||||
@property (nonatomic,strong)NSString *selectedAreaID;
|
||||
|
||||
@end
|
||||
|
||||
@implementation ChinaPlckerView
|
||||
|
||||
/**
|
||||
* 添加城市选择器
|
||||
*
|
||||
* @param delegate 代理
|
||||
* @param views 父视图
|
||||
*
|
||||
* @return 城市选择器
|
||||
*/
|
||||
+(ChinaPlckerView *)customChinaPicker:(id)delegate superView:(UIView*)views{
|
||||
|
||||
UIView *oldView = [views viewWithTag:0x123];
|
||||
if ([oldView isKindOfClass:[ChinaPlckerView class]]) {
|
||||
ChinaPlckerView *navView = (ChinaPlckerView *)oldView;
|
||||
navView.delegate = delegate;
|
||||
[navView showChinaPickerView];
|
||||
return navView;
|
||||
}
|
||||
ChinaPlckerView *navView = [[ChinaPlckerView alloc] init];
|
||||
navView.delegate = delegate;
|
||||
[navView showChinaPickerView];
|
||||
navView.tag = 0x123;
|
||||
[views addSubview:navView];
|
||||
return navView;
|
||||
}
|
||||
/**
|
||||
* 重写父类的init方法
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
- (instancetype)init{
|
||||
if (self = [super init]) {
|
||||
[self initPickerView];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
/**
|
||||
* 确定按钮按下
|
||||
*/
|
||||
- (void)btnClick{
|
||||
if ([_delegate respondsToSelector:@selector(chinaPlckerViewDelegateChinaModel:)]) {
|
||||
[_delegate chinaPlckerViewDelegateChinaModel:self.chinaModel];
|
||||
}
|
||||
[self hideChinaPickerView];
|
||||
}
|
||||
/**
|
||||
* 触摸屏幕其他地方收起选择器
|
||||
*/
|
||||
- (void)tapClick{
|
||||
[self hideChinaPickerView];
|
||||
}
|
||||
/**
|
||||
* 创建城市选择器
|
||||
*/
|
||||
- (void)initPickerView{
|
||||
// 模型的初始化
|
||||
self.chinaModel = [[ChinaArea alloc] init];
|
||||
|
||||
// 默认
|
||||
self.selectedProvinceID = @"2"; // 北京市
|
||||
self.selectedCityID = @"52"; // 北京市
|
||||
self.selectedAreaID = @"500"; // 东城区
|
||||
|
||||
self.chinaModel.provinceModel = ((ProvinceModel *)[self.chinaModel getProvinceDataByID:self.selectedProvinceID]);
|
||||
self.chinaModel.cityModel = ((CityModel *)[self.chinaModel getCityDataByID:self.selectedCityID]);
|
||||
self.chinaModel.areaModel = ((AreaModel *)[self.chinaModel getAreaDataByID:self.selectedAreaID]);
|
||||
|
||||
// 初始化视图
|
||||
self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
|
||||
self.userInteractionEnabled = YES;
|
||||
self.hidden = YES;
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
// 蒙蔽层
|
||||
self.hideView = [[UIView alloc] init];
|
||||
self.hideView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
|
||||
self.hideView.backgroundColor = COLOR(45, 47, 56, 0.0f);
|
||||
[self addSubview:self.hideView];
|
||||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
|
||||
[self.hideView addGestureRecognizer:tap];
|
||||
//添加选择器
|
||||
self.addPickVview = [[UIView alloc]init];
|
||||
self.addPickVview.frame = CGRectMake(0, kScreenHeight - IT_KEYBOARD_HEIGHT, kScreenWidth, IT_KEYBOARD_HEIGHT);
|
||||
self.addPickVview.backgroundColor = [UIColor whiteColor];
|
||||
[self addSubview:self.addPickVview];
|
||||
//确认按钮
|
||||
CGFloat titleHeight = IT_KEYBOARD_HEIGHT - 216;
|
||||
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth,titleHeight)];
|
||||
//titleView.backgroundColor = [UIColor blueColor];
|
||||
[self.addPickVview addSubview:titleView];
|
||||
|
||||
UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
sureBtn.frame = CGRectMake(kScreenWidth - 70, 0, 60, titleHeight);
|
||||
[sureBtn setTitle:@"确定" forState:UIControlStateNormal];
|
||||
[sureBtn setTitle:@"确定" forState:UIControlStateHighlighted];
|
||||
[sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
||||
[sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
|
||||
[sureBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
|
||||
[titleView addSubview:sureBtn];
|
||||
// UIPickerView
|
||||
UIPickerView *pickerView = [[UIPickerView alloc] init];
|
||||
pickerView.frame = CGRectMake(0, CGRectGetMaxY(titleView.frame), kScreenWidth, 216);
|
||||
pickerView.delegate = self;
|
||||
pickerView.dataSource = self;
|
||||
pickerView.backgroundColor = [UIColor whiteColor];
|
||||
[self.addPickVview addSubview:pickerView];
|
||||
self.pickerView = pickerView;
|
||||
|
||||
[self.pickerView selectRow:0 inComponent:0 animated:YES];
|
||||
[self.pickerView selectRow:0 inComponent:1 animated:YES];
|
||||
[self.pickerView selectRow:0 inComponent:2 animated:YES];
|
||||
}
|
||||
#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
|
||||
/**
|
||||
* 设置pickerView的列数
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
*
|
||||
* @return pickerView的列数
|
||||
*/
|
||||
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
|
||||
return 3;
|
||||
}
|
||||
/**
|
||||
* 设置每一列显示的数量
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
* @param component 列
|
||||
*
|
||||
* @return 美列的数量
|
||||
*/
|
||||
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
|
||||
if (component == 0) { // 省份
|
||||
return [self.chinaModel getAllProvinceData].count;
|
||||
}
|
||||
else if (component == 1){ // 城市
|
||||
return [self.chinaModel getCityDataByParentID:self.selectedProvinceID].count;
|
||||
}
|
||||
else{ // 区域
|
||||
return [self.chinaModel getAreaDataByParentID:self.selectedCityID].count;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 设置每一列的宽度
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
* @param component 列
|
||||
*
|
||||
* @return 每列的宽度
|
||||
*/
|
||||
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
|
||||
return (kScreenWidth - 20)/3;
|
||||
}
|
||||
/**
|
||||
* 设置每一行的高度
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
* @param component 列
|
||||
*
|
||||
* @return 每一行的高度
|
||||
*/
|
||||
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
|
||||
return ROW_HEIGHT;
|
||||
}
|
||||
/**
|
||||
* 设置每一行的显示视图
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
* @param row 每一类的row
|
||||
* @param component 列
|
||||
* @param view view description
|
||||
*
|
||||
* @return 每一列每一行的视图
|
||||
*/
|
||||
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
|
||||
if (component == 0) { // 省份
|
||||
if (!view) {
|
||||
view = [[UIView alloc] init];
|
||||
}
|
||||
UILabel *textLabel = [[UILabel alloc] init];
|
||||
textLabel.frame = CGRectMake(0, 0, (kScreenWidth - 20)/3, ROW_HEIGHT);
|
||||
textLabel.textAlignment = NSTextAlignmentCenter;
|
||||
textLabel.text = ((ProvinceModel *)[self.chinaModel getAllProvinceData][row]).NAME;
|
||||
textLabel.font = [UIFont systemFontOfSize:14];
|
||||
textLabel.textColor = COLOR(51, 51, 51, 1);
|
||||
[view addSubview:textLabel];
|
||||
return view;
|
||||
}
|
||||
else if (component == 1){ // 城市
|
||||
if (!view) {
|
||||
view = [[UIView alloc] init];
|
||||
}
|
||||
UILabel *textLabel = [[UILabel alloc] init];
|
||||
textLabel.frame = CGRectMake(0, 0, (kScreenWidth - 20)/3, ROW_HEIGHT);
|
||||
textLabel.textAlignment = NSTextAlignmentCenter;
|
||||
textLabel.text = ((CityModel *)[self.chinaModel getCityDataByParentID:self.selectedProvinceID][row]).NAME;
|
||||
textLabel.font = [UIFont systemFontOfSize:14];
|
||||
textLabel.textColor = COLOR(51, 51, 51, 1);
|
||||
[view addSubview:textLabel];
|
||||
return view;
|
||||
}
|
||||
else{ // 区域
|
||||
if (!view) {
|
||||
view = [[UIView alloc] init];
|
||||
}
|
||||
UILabel *textLabel = [[UILabel alloc] init];
|
||||
textLabel.frame = CGRectMake(0, 0, (kScreenWidth - 20)/3, ROW_HEIGHT);
|
||||
textLabel.textAlignment = NSTextAlignmentCenter;
|
||||
textLabel.text = ((AreaModel *)[self.chinaModel getAreaDataByParentID:self.selectedCityID][row]).NAME;
|
||||
textLabel.font = [UIFont systemFontOfSize:14];
|
||||
textLabel.textColor = COLOR(51, 51, 51, 1);
|
||||
[view addSubview:textLabel];
|
||||
return view;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* pickerView选中代理
|
||||
*
|
||||
* @param pickerView pickerView description
|
||||
* @param row 选中的row
|
||||
* @param component 列
|
||||
*/
|
||||
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
|
||||
switch (component) {
|
||||
case 0:{ // 选择省份
|
||||
self.selectedProvinceID = ((ProvinceModel *)[self.chinaModel getAllProvinceData][row]).ID;
|
||||
[pickerView reloadComponent:1]; // 重载城市
|
||||
[pickerView selectRow:0 inComponent:1 animated:YES];
|
||||
self.chinaModel.provinceModel = (ProvinceModel *)[self.chinaModel getAllProvinceData][row];
|
||||
// 选择了省份就自动定位到该省的第一个市
|
||||
self.selectedCityID = ((CityModel *)[self.chinaModel getCityDataByParentID:self.selectedProvinceID][0]).ID;
|
||||
self.chinaModel.cityModel = (CityModel *)[self.chinaModel getCityDataByParentID:self.selectedProvinceID][0];
|
||||
|
||||
// 选择了省份就自动定位到该省的第一个市的第一个区
|
||||
[pickerView reloadComponent:2]; // 重载区域
|
||||
[pickerView selectRow:0 inComponent:2 animated:YES];
|
||||
// 海南 有的市没有区 真是坑啊
|
||||
if ([self.chinaModel getAreaDataByParentID:self.selectedCityID].count > 0) {
|
||||
self.chinaModel.areaModel = (AreaModel *)[self.chinaModel getAreaDataByParentID:self.selectedCityID][0];
|
||||
}
|
||||
else{
|
||||
self.chinaModel.areaModel.NAME = @"";
|
||||
self.chinaModel.areaModel.ID = @"";
|
||||
self.chinaModel.areaModel.PARENT_AREA_ID = @"9";
|
||||
self.chinaModel.areaModel.GRADE = @"3";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1:{ // 选择城市
|
||||
self.selectedCityID = ((CityModel *)[self.chinaModel getCityDataByParentID:self.selectedProvinceID][row]).ID;
|
||||
[pickerView reloadComponent:2]; // 重载区域
|
||||
[pickerView selectRow:0 inComponent:2 animated:YES];
|
||||
self.chinaModel.cityModel = (CityModel *)[self.chinaModel getCityDataByParentID:self.selectedProvinceID][row];
|
||||
// 选择了市就定位到该市的第一个区
|
||||
// 海南 有的市没有区
|
||||
if ([self.chinaModel getAreaDataByParentID:self.selectedCityID].count > 0) {
|
||||
self.chinaModel.areaModel = (AreaModel *)[self.chinaModel getAreaDataByParentID:self.selectedCityID][0];
|
||||
}
|
||||
else{
|
||||
self.chinaModel.areaModel.NAME = @"";
|
||||
self.chinaModel.areaModel.ID = @"";
|
||||
self.chinaModel.areaModel.PARENT_AREA_ID = @"9";
|
||||
self.chinaModel.areaModel.GRADE = @"3";
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case 2: // 选择区域
|
||||
// 海南 有的市没有区
|
||||
if ([self.chinaModel getAreaDataByParentID:self.selectedCityID].count > 0) {
|
||||
self.chinaModel.areaModel = (AreaModel *)[self.chinaModel getAreaDataByParentID:self.selectedCityID][row];
|
||||
}
|
||||
else{
|
||||
self.chinaModel.areaModel.NAME = @"";
|
||||
self.chinaModel.areaModel.ID = @"";
|
||||
self.chinaModel.areaModel.PARENT_AREA_ID = @"9";
|
||||
self.chinaModel.areaModel.GRADE = @"3";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(chinaPlckerViewDelegateChinaModel:)]) {
|
||||
[_delegate chinaPlckerViewDelegateChinaModel:self.chinaModel];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - 显示
|
||||
-(void)showChinaPickerView
|
||||
{
|
||||
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
|
||||
self.hidden = NO;
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
float pikceY = kScreenHeight - IT_KEYBOARD_HEIGHT;
|
||||
self.addPickVview.frame = CGRectMake(0, pikceY, kScreenWidth, IT_KEYBOARD_HEIGHT);
|
||||
} completion:^(BOOL finished) {
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
self.hideView.backgroundColor = COLOR(45, 47, 56, 0.2f);
|
||||
} completion:^(BOOL finished) {
|
||||
|
||||
}];
|
||||
}];
|
||||
}
|
||||
#pragma mark - 隐藏
|
||||
-(void)hideChinaPickerView
|
||||
{
|
||||
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
float pikceY = kScreenHeight;
|
||||
self.addPickVview.frame = CGRectMake(0, pikceY, kScreenWidth, IT_KEYBOARD_HEIGHT);
|
||||
} completion:^(BOOL finished) {
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
self.hideView.backgroundColor = COLOR(45, 47, 56, 0.0f);
|
||||
} completion:^(BOOL finished) {
|
||||
self.hidden = YES;
|
||||
}];
|
||||
}];
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(chinaPickerViewDelegateHidden)]) {
|
||||
[_delegate chinaPickerViewDelegateHidden];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Only override drawRect: if you perform custom drawing.
|
||||
// An empty implementation adversely affects performance during animation.
|
||||
- (void)drawRect:(CGRect)rect {
|
||||
// Drawing code
|
||||
}
|
||||
*/
|
||||
|
||||
@end
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// CityModel.h
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface CityModel : NSObject
|
||||
/**
|
||||
* GRADE
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *GRADE;
|
||||
/**
|
||||
* 城市ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *ID;
|
||||
/**
|
||||
* 城市名
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *NAME;
|
||||
/**
|
||||
* 城市的上一级ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *PARENT_AREA_ID;
|
||||
@end
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// CityModel.m
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "CityModel.h"
|
||||
|
||||
@implementation CityModel
|
||||
|
||||
@end
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// ProvinceModel.h
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface ProvinceModel : NSObject
|
||||
/**
|
||||
* GRADE
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *GRADE;
|
||||
/**
|
||||
* 省份ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *ID;
|
||||
/**
|
||||
* 省份名
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *NAME;
|
||||
/**
|
||||
* 省份的上一级ID
|
||||
*/
|
||||
@property (nonatomic,copy)NSString *PARENT_AREA_ID;
|
||||
@end
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// ProvinceModel.m
|
||||
// 地址选择器
|
||||
//
|
||||
// Created by zhuming on 16/2/15.
|
||||
// Copyright © 2016年 zhuming. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ProvinceModel.h"
|
||||
|
||||
@implementation ProvinceModel
|
||||
|
||||
@end
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user