IOS UIPickView+sqlite 选择中国全部城市案例
1.案例简单介绍
2.城市对象模型
//
// CityModel.h
// readData
//
// Created by 赵超 on 14-8-28.
// Copyright (c) 2014年 赵超. All rights reserved.
// #import <Foundation/Foundation.h> @interface CityModel : NSObject @property (nonatomic,copy) NSString *pid; //父级城市ID
@property (nonatomic,copy) NSString *cityName; //城市名
@property (nonatomic,copy) NSString *ids; //城市ID @end
3.城市数据库操作对象
//
// CityDB.h
// readData
//
// Created by 赵超 on 14-8-28.
// Copyright (c) 2014年 赵超. All rights reserved.
// #import "BaseDB.h"
#import "CityModel.h" @interface CityDB : BaseDB
/**
*CityDB单例
*/
+(id)ShareDB; /**
* 创建数据库
* dbName:数据库名称
*/
-(void)creatTableWithDataBaseName:(NSString*) dbName; /**
* 添加一个城市
* city:城市
* dbName:数据库名称
*/
-(BOOL)addCity:(CityModel*)city dbName:(NSString*)dbName;
/**
* 选择全部的城市
* dbName:数据库名称
*/
-(id)selectAllCity:(NSString*)dbName;
/**
* 选择全部的省份
* dbName:数据库名称
*/
-(id)selectAllProvince:(NSString *)dbName;
/**
* 删除全部城市
* dbName:数据库名称
*/
-(BOOL)deleteAllCity:(NSString*)dbName;
/**
* 通过上一级省份选择下级市
* city:上一级城市
* dbName:数据库名称
*/
-(id)selectCityByProvince:(CityModel*)provice dbName:(NSString*)dbName; @end
CityDB.m文件实现
//
// CityDB.m
// readData
//
// Created by 赵超 on 14-8-28.
// Copyright (c) 2014年 赵超. All rights reserved.
// #import "CityDB.h" @implementation CityDB static CityDB *citydb; +(id)ShareDB{
if (citydb==nil) {
citydb=[[CityDB alloc] init];
}
return citydb;
} -(void)creatTableWithDataBaseName:(NSString *)dbName{
NSString *sql=@"create table china (ids text primary key,cityName text,pid text )";
[self createTable:sql dataBaseName:dbName];
} -(BOOL)addCity:(CityModel *)city dbName:(NSString *)dbName{
NSString *sql=@"insert into china values (? ,?,?)";
NSArray *params=@[city.ids,city.cityName,city.pid];
return [self execSql:sql parmas:params dataBaseName:dbName];
} -(id)selectAllCity:(NSString *)dbName{
NSString *sql=@"select ids,cityName,pid from china";
return [self selectCity:sql parmas:nil dbName:dbName];
} -(id)selectCity:(NSString*)sql parmas:(NSArray*)params dbName:(NSString*)dbName{
NSArray *result= [self selectSql:sql parmas:params dataBaseName:dbName];
NSMutableArray *citys=[NSMutableArray array];
for (NSDictionary *dic in result) {
CityModel *city=[[CityModel alloc]init];
city.ids=[dic objectForKey:@"ids"];
city.cityName=[dic objectForKey:@"cityName"];
city.pid=[dic objectForKey:@"pid"];
[citys addObject:city];
}
return citys;
} -(id)selectAllProvince:(NSString *)dbName{
NSString *sql=@"select ids,cityName,pid from china where pid=?";
NSArray *parmas=@[@"0"];
return [self selectCity:sql parmas:parmas dbName:dbName];
} -(id)selectCityByProvince:(CityModel *)provice dbName:(NSString *)dbName{
NSString *sql=@"select * from china where pid=?";
NSArray *params=@[provice.ids];
return [self selectCity:sql parmas:params dbName:dbName];
} -(BOOL)deleteAllCity:(NSString *)dbName{
NSString *sql=@"delete from china";
return [self execSql:sql parmas:nil dataBaseName:dbName];
} @end
4.城市数据处理
//调用CitDB对象向数据库中添加一个城市
-(void)addCity:(CityModel* )city{
[[CityDB ShareDB] addCity:city dbName:@"China.sqlite"];
}
//处理china.txt城市数据。将其写入数据库中
-(void)readData{
NSString *path=[[NSBundle mainBundle] pathForResource:@"china" ofType:@"txt"];
NSLog(@"%@",path);
char pid[30],name[30],ids[30]; FILE *f=fopen([path UTF8String], "r");
int i=0;
while (!feof(f)) {
CityModel *city=[[CityModel alloc] init];
fscanf(f, " %s %s %s ",ids,name,pid);
NSString *pids=[NSString stringWithUTF8String:pid];
NSString *names=[NSString stringWithUTF8String:name];
NSString *idss=[NSString stringWithUTF8String:ids];
city.ids=idss;
city.pid=pids;
city.cityName=names;
//向数据库插入一个城市
[self addCity:city];
NSLog(@"%@ %@ %@ %d",pids,names,idss,++i); }
}
5.UIPickView显示数据
@interface MainViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>{
CityModel *privceModel; //选择的省
CityModel *cityModel; //选择的市
CityModel *subCityModel; //选择的地级市
CityModel *areaModel; //选择的区
UILabel *selectCity; //显示选择的结果
} @property (nonatomic,retain) NSArray *privices; //全部省份 @property (nonatomic,retain) NSArray *citys; //省下相应的市 @property (nonatomic,retain) NSArray *subCitys; //市下相应的地级市 @property (nonatomic,retain) NSArray *area; //区 @end
在MainViewController的viewDidLoad中加入UIPickView并初始化数据
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor grayColor]; UIPickerView *pickView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
pickView.dataSource=self;
pickView.delegate=self;
pickView.showsSelectionIndicator=YES;
pickView.backgroundColor=[UIColor whiteColor]; [self.view addSubview:pickView];
//初始化数据
self.privices=[[CityDB ShareDB] selectAllProvince:dataBaseName];
CityModel *city=[self.privices objectAtIndex:0];
self.citys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];
city=[self.citys objectAtIndex:0];
self.subCitys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];
city=[self.citys objectAtIndex:0];
self.area=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName]; selectCity=[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 320, 30)];
}
实现UIPickView的列数和行数代理函数。列数仅仅有4列。第一列的行数是中国全部的省数所以中人返回self.privices.count就能够了。可是第二列必需知道第一列选中哪个省份后,再通过这个省份从数据库库查出以下的市才知道要显示的行数。第3列是基于第2列选中的行数,第4列是基于第3列选中的列数。实现代码例如以下:
//UIPcikView总共4列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 4;
} //为每列载入行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component==0) {
return self.privices.count;
}else
if (component==1) {
//获取第一列选中的省份列表
NSInteger privoceIndex=[pickerView selectedRowInComponent:0];
CityModel *privoice=[self.privices objectAtIndex:privoceIndex];
//从数据库中查询,省份以下的市
self.citys=[[CityDB ShareDB] selectCityByProvince:privoice dbName:dataBaseName];
//返回市的个数
return self.citys.count; }else
if (component==2) {
NSInteger cityIndex=[pickerView selectedRowInComponent:1];
if (self.citys.count==0) {
return 0;
}
CityModel *subCitys=[self.citys objectAtIndex:cityIndex];
self.subCitys=[[CityDB ShareDB] selectCityByProvince:subCitys dbName:dataBaseName];
return self.subCitys.count; }else
if (component==3) {
NSInteger subCityIndex=[pickerView selectedRowInComponent:2];
if (self.subCitys.count==0) {
return 0;
}
CityModel *ares=[self.subCitys objectAtIndex:subCityIndex];
self.area=[[CityDB ShareDB] selectCityByProvince:ares dbName:dataBaseName];
return self.area.count; }else{
return 0;
} }
为UIPickView载入每行每列的数据,获取数据时要注意有推断是否为空
//获取每列每行的名称
-(NSString*)getCityName:(NSInteger)row componet:(NSInteger) component{
if (component==0) {
CityModel *city=[self.privices objectAtIndex:row];
return city.cityName;
}else if (component==1) {
CityModel *city=[self.citys objectAtIndex:row];
return city.cityName;
}else if (component==2) {
if (self.subCitys==nil) {
return @"";
}else{
CityModel *city=[self.subCitys objectAtIndex:row];
return city.cityName;
}
}
else if (component==3) {
if (self.area==nil) {
return @"";
}else{
CityModel *city=[self.area objectAtIndex:row];
return city.cityName;
} } return @"";
} -(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
//获取名称
lable.text=[self getCityName:row componet:component];
lable.font=[UIFont systemFontOfSize:14]; return lable;
}
最后实现UIPickView的选择响应事件刷新Pickview。并显示选择的结果
//获取每列每行的名称
-(NSString*)getCityName:(NSInteger)row componet:(NSInteger) component{
if (component==0) {
CityModel *city=[self.privices objectAtIndex:row];
return city.cityName;
}else if (component==1) {
CityModel *city=[self.citys objectAtIndex:row];
return city.cityName;
}else if (component==2) {
if (self.subCitys==nil) {
return @"";
}else{
CityModel *city=[self.subCitys objectAtIndex:row];
return city.cityName;
}
}
else if (component==3) {
if (self.area==nil) {
return @"";
}else{
CityModel *city=[self.area objectAtIndex:row];
return city.cityName;
} } return @"";
} -(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
//获取名称
lable.text=[self getCityName:row componet:component];
lable.font=[UIFont systemFontOfSize:14]; return lable;
}
项目完整projecthttps://github.com/whzhaochao/IOSChinaCity
IOS UIPickView+sqlite 选择中国全部城市案例的更多相关文章
- JS前端三维地球渲染——中国各城市航空路线展示
前言 我还从来没有写过有关纯JS的文章(上次的矢量瓦片展示除外,相对较简单.),自己也学习过JS.CSS等前端知识,了解JQuery.React等框架,但是自己艺术天分实在不过关,不太喜欢前端设计,比 ...
- Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...
- 中国各城市PM2.5数据间的相关分析
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- iOS开发--SQLite重要框架FMDB的使用
什么是FMDB: FMDB是一个和iOS的SQLite数据库操作相关的第三方框架.主要把C语言操作数据库的代码用OC进行了封装.使用者只需调用该框架的API就能用来创建并连接数据库,创建表,查询等. ...
- [RN] 全国城市列表选择 (包含定位城市、热门城市、全国城市)
全国城市列表选择 (包含定位城市.热门城市.全国城市) 用ScrollView 实现,解决 SectionList 实现的卡顿问题 实现效果如图: 代码实现如图: 主逻辑文件 cityList.js ...
- IOS Sqlite用户界面增删改查案例
1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...
- iOS界面设计,12个优秀案例激发你的灵感
总所周知,iOS和Android是当今两大移动平台,前者采用Human Interface Design,后者采用Material Design.作为设计师,尤其是App设计师,总是会在这两者进行设计 ...
- iOS开发——高级篇——iOS中如何选择delegate、通知、KVO(以及三者的区别)
在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers[B]怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信:1委托delegation2通知 ...
- iOS中如何选择delegate、通知、KVO(以及三者的区别)
转载自:http://blog.csdn.net/dqjyong/article/details/7685933 在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,contr ...
随机推荐
- day20-python之装饰器
1.装饰器 #!/usr/bin/env python # -*- coding:utf-8 -*- import time def cal(l): start_time=time.time() re ...
- 总线(bus);设备(devices);驱动(drivers)
Linux Cross Reference Free Electrons Embedded Linux Experts • Source Navigation • Diff Markup • Id ...
- UVa-208 Firetruck (图的DFS)
UVA-208 天道好轮回.UVA饶过谁. 就是一个图的DFS. 不过这个图的边太多,要事先判一下起点和终点是否联通(我喜欢用并查集),否则会TLE. #include <iostream> ...
- eclipse去除js(JavaScript)验证错误
第一步: 去除eclipse的JS验证: 将windows->preference->Java Script->Validator->Errors/Warnings-> ...
- 2019年最新 Python 模拟登录知乎 支持验证码
知乎的登录页面已经改版多次,加强了身份验证,网络上大部分模拟登录均已失效,所以我重写了一份完整的,并实现了提交验证码 (包括中文验证码),本文我对分析过程和代码进行步骤分解,完整的代码请见末尾 Git ...
- Hive 启动报错,需先启动元数据
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...
- chromedriver 下载
https://sites.google.com/a/chromium.org/chromedriver/downloads 百度网盘链接:https://pan.baidu.com/s/1nwL ...
- Python开发:网络编程
Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别的网络 ...
- POJ 2155 Matrix【二维线段树】
题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...
- 刷题总结——作诗(bzoj2821)
题目: Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章, ...