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 ...
随机推荐
- Dajngo admin
Dajngo admin admin app Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.p ...
- python动态添加属性和方法
---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...
- Python语言程序设计之三--列表List常见操作和错误总结
最近在学习列表,在这里卡住了很久,主要是课后习题太多,而且难度也不小.像我看的这本<Python语言程序设计>--梁勇著,列表和多维列表两章课后习题就有93道之多.我的天!但是题目出的非常 ...
- H.264 与 MPEG-4 压缩格式的变革
h.264 和 mpeg-4 的关系: h.264 /avc ( advanced video coding )标准,是 mpeg-4 的第 10 部分. mpeg-4的初衷是将dvd质量的图像码流从 ...
- 【10】css hack原理及常用hack
[10]css hack原理及常用hack 原理:利用不同浏览器对CSS的支持和解析结果不一样编写针对特定浏览器样式.常见的hack有1)属性hack.2)选择器hack.3)IE条件注释 IE条件注 ...
- IOS 自动布局-UIStackPanel和UIGridPanel(五)
试想这样的一个需求场合,一个button靠右显示,并且距离superView的顶部和右边间距分别为10和5.如下图所示: 要实现这样的需求,如果不用自动布局技术,那么我们能想到的就是老老实实的使用绝对 ...
- 九度oj 题目1254:N皇后问题
题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上.因为皇后可以直走,横走和斜走如下图). 你的任务是,对 ...
- EasyUI 加载Tree
function LoadTree(result) { mainMenu = $('#mainMenu').tree({ url: "/ajax/GetTreeJson.ashx" ...
- ajax dome案例
一.首先HTML页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- mysql的row_number()实现
在mysql中没有row_number()方法,这里模拟row_number()实现: 如有表 studentid name age class1 张1 15 12 张2 15 ...