1.案例简单介绍

通过读取文件。将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的

2.城市对象模型

中国全部城市数据请看http://blog.csdn.net/whzhaochao/article/details/37969145,城市模型对象例如以下
  1. //
  2. // CityModel.h
  3. // readData
  4. //
  5. // Created by 赵超 on 14-8-28.
  6. // Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface CityModel : NSObject
  12.  
  13. @property (nonatomic,copy) NSString *pid; //父级城市ID
  14. @property (nonatomic,copy) NSString *cityName; //城市名
  15. @property (nonatomic,copy) NSString *ids; //城市ID
  16.  
  17. @end

3.城市数据库操作对象

sqlite封操作BseDB类请看http://blog.csdn.net/whzhaochao/article/details/38865535,然后封装城市对象的数据库操作对象CityDB
CityDB.h文件
  1. //
  2. // CityDB.h
  3. // readData
  4. //
  5. // Created by 赵超 on 14-8-28.
  6. // Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8.  
  9. #import "BaseDB.h"
  10. #import "CityModel.h"
  11.  
  12. @interface CityDB : BaseDB
  13. /**
  14. *CityDB单例
  15. */
  16. +(id)ShareDB;
  17.  
  18. /**
  19. * 创建数据库
  20. * dbName:数据库名称
  21. */
  22. -(void)creatTableWithDataBaseName:(NSString*) dbName;
  23.  
  24. /**
  25. * 添加一个城市
  26. * city:城市
  27. * dbName:数据库名称
  28. */
  29. -(BOOL)addCity:(CityModel*)city dbName:(NSString*)dbName;
  30. /**
  31. * 选择全部的城市
  32. * dbName:数据库名称
  33. */
  34. -(id)selectAllCity:(NSString*)dbName;
  35. /**
  36. * 选择全部的省份
  37. * dbName:数据库名称
  38. */
  39. -(id)selectAllProvince:(NSString *)dbName;
  40. /**
  41. * 删除全部城市
  42. * dbName:数据库名称
  43. */
  44. -(BOOL)deleteAllCity:(NSString*)dbName;
  45. /**
  46. * 通过上一级省份选择下级市
  47. * city:上一级城市
  48. * dbName:数据库名称
  49. */
  50. -(id)selectCityByProvince:(CityModel*)provice dbName:(NSString*)dbName;
  51.  
  52. @end

CityDB.m文件实现

  1. //
  2. // CityDB.m
  3. // readData
  4. //
  5. // Created by 赵超 on 14-8-28.
  6. // Copyright (c) 2014年 赵超. All rights reserved.
  7. //
  8.  
  9. #import "CityDB.h"
  10.  
  11. @implementation CityDB
  12.  
  13. static CityDB *citydb;
  14.  
  15. +(id)ShareDB{
  16. if (citydb==nil) {
  17. citydb=[[CityDB alloc] init];
  18. }
  19. return citydb;
  20. }
  21.  
  22. -(void)creatTableWithDataBaseName:(NSString *)dbName{
  23. NSString *sql=@"create table china (ids text primary key,cityName text,pid text )";
  24. [self createTable:sql dataBaseName:dbName];
  25. }
  26.  
  27. -(BOOL)addCity:(CityModel *)city dbName:(NSString *)dbName{
  28. NSString *sql=@"insert into china values (?
  29.  
  30. ,?,?)";
  31. NSArray *params=@[city.ids,city.cityName,city.pid];
  32. return [self execSql:sql parmas:params dataBaseName:dbName];
  33. }
  34.  
  35. -(id)selectAllCity:(NSString *)dbName{
  36. NSString *sql=@"select ids,cityName,pid from china";
  37. return [self selectCity:sql parmas:nil dbName:dbName];
  38. }
  39.  
  40. -(id)selectCity:(NSString*)sql parmas:(NSArray*)params dbName:(NSString*)dbName{
  41. NSArray *result= [self selectSql:sql parmas:params dataBaseName:dbName];
  42. NSMutableArray *citys=[NSMutableArray array];
  43. for (NSDictionary *dic in result) {
  44. CityModel *city=[[CityModel alloc]init];
  45. city.ids=[dic objectForKey:@"ids"];
  46. city.cityName=[dic objectForKey:@"cityName"];
  47. city.pid=[dic objectForKey:@"pid"];
  48. [citys addObject:city];
  49. }
  50. return citys;
  51. }
  52.  
  53. -(id)selectAllProvince:(NSString *)dbName{
  54. NSString *sql=@"select ids,cityName,pid from china where pid=?";
  55. NSArray *parmas=@[@"0"];
  56. return [self selectCity:sql parmas:parmas dbName:dbName];
  57. }
  58.  
  59. -(id)selectCityByProvince:(CityModel *)provice dbName:(NSString *)dbName{
  60. NSString *sql=@"select * from china where pid=?";
  61. NSArray *params=@[provice.ids];
  62. return [self selectCity:sql parmas:params dbName:dbName];
  63. }
  64.  
  65. -(BOOL)deleteAllCity:(NSString *)dbName{
  66. NSString *sql=@"delete from china";
  67. return [self execSql:sql parmas:nil dataBaseName:dbName];
  68. }
  69.  
  70. @end

4.城市数据处理

中国城市数据放在china.txt中,须要处理后写入数据库中。读取文件装数据写入数据库代码例如以下
  1. //调用CitDB对象向数据库中添加一个城市
  2. -(void)addCity:(CityModel* )city{
  3. [[CityDB ShareDB] addCity:city dbName:@"China.sqlite"];
  4. }
  5. //处理china.txt城市数据。将其写入数据库中
  6. -(void)readData{
  7. NSString *path=[[NSBundle mainBundle] pathForResource:@"china" ofType:@"txt"];
  8. NSLog(@"%@",path);
  9. char pid[30],name[30],ids[30];
  10.  
  11. FILE *f=fopen([path UTF8String], "r");
  12. int i=0;
  13. while (!feof(f)) {
  14. CityModel *city=[[CityModel alloc] init];
  15. fscanf(f, " %s %s %s ",ids,name,pid);
  16. NSString *pids=[NSString stringWithUTF8String:pid];
  17. NSString *names=[NSString stringWithUTF8String:name];
  18. NSString *idss=[NSString stringWithUTF8String:ids];
  19. city.ids=idss;
  20. city.pid=pids;
  21. city.cityName=names;
  22. //向数据库插入一个城市
  23. [self addCity:city];
  24. NSLog(@"%@ %@ %@ %d",pids,names,idss,++i);
  25.  
  26. }
  27. }


5.UIPickView显示数据

MainViewControoler用户数据的显示。其.h文件内容例如以下

  1. @interface MainViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>{
  2. CityModel *privceModel; //选择的省
  3. CityModel *cityModel; //选择的市
  4. CityModel *subCityModel; //选择的地级市
  5. CityModel *areaModel; //选择的区
  6. UILabel *selectCity; //显示选择的结果
  7. }
  8.  
  9. @property (nonatomic,retain) NSArray *privices; //全部省份
  10.  
  11. @property (nonatomic,retain) NSArray *citys; //省下相应的市
  12.  
  13. @property (nonatomic,retain) NSArray *subCitys; //市下相应的地级市
  14.  
  15. @property (nonatomic,retain) NSArray *area; //区
  16.  
  17. @end

在MainViewController的viewDidLoad中加入UIPickView并初始化数据

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view.
  5. self.view.backgroundColor=[UIColor grayColor];
  6.  
  7. UIPickerView *pickView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
  8. pickView.dataSource=self;
  9. pickView.delegate=self;
  10. pickView.showsSelectionIndicator=YES;
  11. pickView.backgroundColor=[UIColor whiteColor];
  12.  
  13. [self.view addSubview:pickView];
  14. //初始化数据
  15. self.privices=[[CityDB ShareDB] selectAllProvince:dataBaseName];
  16. CityModel *city=[self.privices objectAtIndex:0];
  17. self.citys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];
  18. city=[self.citys objectAtIndex:0];
  19. self.subCitys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];
  20. city=[self.citys objectAtIndex:0];
  21. self.area=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];
  22.  
  23. selectCity=[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 320, 30)];
  24. }

实现UIPickView的列数和行数代理函数。列数仅仅有4列。第一列的行数是中国全部的省数所以中人返回self.privices.count就能够了。可是第二列必需知道第一列选中哪个省份后,再通过这个省份从数据库库查出以下的市才知道要显示的行数。第3列是基于第2列选中的行数,第4列是基于第3列选中的列数。实现代码例如以下:

  1. //UIPcikView总共4列
  2. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  3. return 4;
  4. }
  5.  
  6. //为每列载入行数
  7. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  8.  
  9. if (component==0) {
  10. return self.privices.count;
  11. }else
  12. if (component==1) {
  13. //获取第一列选中的省份列表
  14. NSInteger privoceIndex=[pickerView selectedRowInComponent:0];
  15. CityModel *privoice=[self.privices objectAtIndex:privoceIndex];
  16. //从数据库中查询,省份以下的市
  17. self.citys=[[CityDB ShareDB] selectCityByProvince:privoice dbName:dataBaseName];
  18. //返回市的个数
  19. return self.citys.count;
  20.  
  21. }else
  22. if (component==2) {
  23. NSInteger cityIndex=[pickerView selectedRowInComponent:1];
  24. if (self.citys.count==0) {
  25. return 0;
  26. }
  27. CityModel *subCitys=[self.citys objectAtIndex:cityIndex];
  28. self.subCitys=[[CityDB ShareDB] selectCityByProvince:subCitys dbName:dataBaseName];
  29. return self.subCitys.count;
  30.  
  31. }else
  32. if (component==3) {
  33. NSInteger subCityIndex=[pickerView selectedRowInComponent:2];
  34. if (self.subCitys.count==0) {
  35. return 0;
  36. }
  37. CityModel *ares=[self.subCitys objectAtIndex:subCityIndex];
  38. self.area=[[CityDB ShareDB] selectCityByProvince:ares dbName:dataBaseName];
  39. return self.area.count;
  40.  
  41. }else{
  42. return 0;
  43. }
  44.  
  45. }

为UIPickView载入每行每列的数据,获取数据时要注意有推断是否为空

  1. //获取每列每行的名称
  2. -(NSString*)getCityName:(NSInteger)row componet:(NSInteger) component{
  3. if (component==0) {
  4. CityModel *city=[self.privices objectAtIndex:row];
  5. return city.cityName;
  6. }else if (component==1) {
  7. CityModel *city=[self.citys objectAtIndex:row];
  8. return city.cityName;
  9. }else if (component==2) {
  10. if (self.subCitys==nil) {
  11. return @"";
  12. }else{
  13. CityModel *city=[self.subCitys objectAtIndex:row];
  14. return city.cityName;
  15. }
  16. }
  17. else if (component==3) {
  18. if (self.area==nil) {
  19. return @"";
  20. }else{
  21. CityModel *city=[self.area objectAtIndex:row];
  22. return city.cityName;
  23. }
  24.  
  25. }
  26.  
  27. return @"";
  28. }
  29.  
  30. -(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  31.  
  32. UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
  33. //获取名称
  34. lable.text=[self getCityName:row componet:component];
  35. lable.font=[UIFont systemFontOfSize:14];
  36.  
  37. return lable;
  38. }

最后实现UIPickView的选择响应事件刷新Pickview。并显示选择的结果

  1. //获取每列每行的名称
  2. -(NSString*)getCityName:(NSInteger)row componet:(NSInteger) component{
  3. if (component==0) {
  4. CityModel *city=[self.privices objectAtIndex:row];
  5. return city.cityName;
  6. }else if (component==1) {
  7. CityModel *city=[self.citys objectAtIndex:row];
  8. return city.cityName;
  9. }else if (component==2) {
  10. if (self.subCitys==nil) {
  11. return @"";
  12. }else{
  13. CityModel *city=[self.subCitys objectAtIndex:row];
  14. return city.cityName;
  15. }
  16. }
  17. else if (component==3) {
  18. if (self.area==nil) {
  19. return @"";
  20. }else{
  21. CityModel *city=[self.area objectAtIndex:row];
  22. return city.cityName;
  23. }
  24.  
  25. }
  26.  
  27. return @"";
  28. }
  29.  
  30. -(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  31.  
  32. UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
  33. //获取名称
  34. lable.text=[self getCityName:row componet:component];
  35. lable.font=[UIFont systemFontOfSize:14];
  36.  
  37. return lable;
  38. }

项目完整projecthttps://github.com/whzhaochao/IOSChinaCity







IOS UIPickView+sqlite 选择中国全部城市案例的更多相关文章

  1. JS前端三维地球渲染——中国各城市航空路线展示

    前言 我还从来没有写过有关纯JS的文章(上次的矢量瓦片展示除外,相对较简单.),自己也学习过JS.CSS等前端知识,了解JQuery.React等框架,但是自己艺术天分实在不过关,不太喜欢前端设计,比 ...

  2. Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...

  3. 中国各城市PM2.5数据间的相关分析

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  4. iOS开发--SQLite重要框架FMDB的使用

    什么是FMDB: FMDB是一个和iOS的SQLite数据库操作相关的第三方框架.主要把C语言操作数据库的代码用OC进行了封装.使用者只需调用该框架的API就能用来创建并连接数据库,创建表,查询等. ...

  5. [RN] 全国城市列表选择 (包含定位城市、热门城市、全国城市)

    全国城市列表选择 (包含定位城市.热门城市.全国城市) 用ScrollView 实现,解决 SectionList 实现的卡顿问题 实现效果如图: 代码实现如图: 主逻辑文件 cityList.js ...

  6. IOS Sqlite用户界面增删改查案例

    1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...

  7. iOS界面设计,12个优秀案例激发你的灵感

    总所周知,iOS和Android是当今两大移动平台,前者采用Human Interface Design,后者采用Material Design.作为设计师,尤其是App设计师,总是会在这两者进行设计 ...

  8. iOS开发——高级篇——iOS中如何选择delegate、通知、KVO(以及三者的区别)

      在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers[B]怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信:1委托delegation2通知 ...

  9. iOS中如何选择delegate、通知、KVO(以及三者的区别)

    转载自:http://blog.csdn.net/dqjyong/article/details/7685933 在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,contr ...

随机推荐

  1. Dajngo admin

    Dajngo admin admin app Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.p ...

  2. python动态添加属性和方法

    ---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...

  3. Python语言程序设计之三--列表List常见操作和错误总结

    最近在学习列表,在这里卡住了很久,主要是课后习题太多,而且难度也不小.像我看的这本<Python语言程序设计>--梁勇著,列表和多维列表两章课后习题就有93道之多.我的天!但是题目出的非常 ...

  4. H.264 与 MPEG-4 压缩格式的变革

    h.264 和 mpeg-4 的关系: h.264 /avc ( advanced video coding )标准,是 mpeg-4 的第 10 部分. mpeg-4的初衷是将dvd质量的图像码流从 ...

  5. 【10】css hack原理及常用hack

    [10]css hack原理及常用hack 原理:利用不同浏览器对CSS的支持和解析结果不一样编写针对特定浏览器样式.常见的hack有1)属性hack.2)选择器hack.3)IE条件注释 IE条件注 ...

  6. IOS 自动布局-UIStackPanel和UIGridPanel(五)

    试想这样的一个需求场合,一个button靠右显示,并且距离superView的顶部和右边间距分别为10和5.如下图所示: 要实现这样的需求,如果不用自动布局技术,那么我们能想到的就是老老实实的使用绝对 ...

  7. 九度oj 题目1254:N皇后问题

    题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上.因为皇后可以直走,横走和斜走如下图). 你的任务是,对 ...

  8. EasyUI 加载Tree

    function LoadTree(result) { mainMenu = $('#mainMenu').tree({ url: "/ajax/GetTreeJson.ashx" ...

  9. ajax dome案例

    一.首先HTML页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  10. mysql的row_number()实现

    在mysql中没有row_number()方法,这里模拟row_number()实现: 如有表 studentid   name age   class1 张1 15     12 张2 15     ...