IOS UIPickView+sqlite 选择中国所有城市案例

目录(?)[+]

1.案例简介

通过读取文件,将中国所有城市写入sqlite数据库中,现通过UIPickView实现中国所有城市的选择,效果图如下所示

2.城市对象模型

中国所有城市数据请看 http://blog.csdn.net/whzhaochao/article/details/37969145,城市模型对象如下
[objc]  view plain  copy
  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文件
[objc]  view plain  copy
  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.   
  53. @end  
CityDB.m文件实现
[objc]  view plain  copy
  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.     NSArray *params=@[city.ids,city.cityName,city.pid];  
  30.     return [self execSql:sql parmas:params dataBaseName:dbName];  
  31. }  
  32.   
  33. -(id)selectAllCity:(NSString *)dbName{  
  34.     NSString *sql=@"select ids,cityName,pid from china";  
  35.     return [self selectCity:sql parmas:nil dbName:dbName];  
  36. }  
  37.   
  38. -(id)selectCity:(NSString*)sql parmas:(NSArray*)params dbName:(NSString*)dbName{  
  39.     NSArray *result= [self selectSql:sql parmas:params dataBaseName:dbName];  
  40.     NSMutableArray *citys=[NSMutableArray array];  
  41.     for (NSDictionary *dic in result) {  
  42.         CityModel *city=[[CityModel alloc]init];  
  43.         city.ids=[dic objectForKey:@"ids"];  
  44.         city.cityName=[dic objectForKey:@"cityName"];  
  45.         city.pid=[dic objectForKey:@"pid"];  
  46.         [citys addObject:city];  
  47.     }  
  48.     return citys;  
  49. }  
  50.   
  51. -(id)selectAllProvince:(NSString *)dbName{  
  52.     NSString *sql=@"select ids,cityName,pid from china where pid=?";  
  53.     NSArray  *parmas=@[@"0"];  
  54.     return [self selectCity:sql parmas:parmas dbName:dbName];  
  55. }  
  56.   
  57. -(id)selectCityByProvince:(CityModel *)provice dbName:(NSString *)dbName{  
  58.     NSString *sql=@"select * from china where pid=?";  
  59.     NSArray  *params=@[provice.ids];  
  60.     return [self selectCity:sql parmas:params dbName:dbName];  
  61. }  
  62.   
  63. -(BOOL)deleteAllCity:(NSString *)dbName{  
  64.     NSString *sql=@"delete from china";  
  65.     return [self execSql:sql parmas:nil dataBaseName:dbName];  
  66. }  
  67.   
  68. @end  

4.城市数据处理

中国城市数据放在china.txt中,需要处理后写入数据库中,读取文件装数据写入数据库代码如下
[objc]  view plain  copy
  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.     FILEFILE *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文件内容如下

[objc]  view plain  copy
  1. @interface MainViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>{  
  2.     CityModel *privceModel;     //选择的省  
  3.     CityModel *cityModel;       //选择的市  
  4.     CityModel *subCityModel;    //选择的地级市  
  5.     CityModel *areaModel;       //选择的区  
  6.     UILabel *selectCity;        //显示选择的结果  
  7. }  
  8.   
  9.   
  10. @property (nonatomic,retainNSArray *privices;    //所有省份  
  11.   
  12. @property (nonatomic,retainNSArray *citys;       //省下对应的市  
  13.   
  14. @property (nonatomic,retainNSArray *subCitys;    //市下对应的地级市  
  15.   
  16. @property (nonatomic,retainNSArray *area;        //区  
  17.   
  18.   
  19. @end  

在MainViewController的viewDidLoad中添加UIPickView并初始化数据
[objc]  view plain  copy
  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(010000)];  
  8.     pickView.dataSource=self;  
  9.     pickView.delegate=self;  
  10.     pickView.showsSelectionIndicator=YES;  
  11.     pickView.backgroundColor=[UIColor whiteColor];  
  12.   
  13.       
  14.     [self.view addSubview:pickView];  
  15.     //初始化数据  
  16.     self.privices=[[CityDB ShareDB] selectAllProvince:dataBaseName];  
  17.     CityModel *city=[self.privices objectAtIndex:0];  
  18.     self.citys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];  
  19.     city=[self.citys objectAtIndex:0];  
  20.     self.subCitys=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];  
  21.     city=[self.citys objectAtIndex:0];  
  22.     self.area=[[CityDB ShareDB] selectCityByProvince:city dbName:dataBaseName];  
  23.       
  24.     selectCity=[[UILabel alloc] initWithFrame:CGRectMake(04032030)];  
  25. }  

实现UIPickView的列数和行数代理函数,列数只有4列,第一列的行数是中国所有的省数所以中人返回self.privices.count就可以了,但是第二列必需知道第一列选中哪个省份后,再通过这个省份从数据库库查出下面的市才知道要显示的行数,第3列是基于第2列选中的行数,第4列是基于第3列选中的列数,实现代码如下:
[objc]  view plain  copy
  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加载每行每列的数据,获取数据时要注意有判断是否为空
[objc]  view plain  copy
  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(006030)];  
  33.     //获取名称  
  34.     lable.text=[self getCityName:row componet:component];  
  35.     lable.font=[UIFont systemFontOfSize:14];  
  36.       
  37.     return lable;  
  38. }  
最后实现UIPickView的选择响应事件刷新Pickview,并显示选择的结果
[objc]  view plain  copy
  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(006030)];  
  33.     //获取名称  
  34.     lable.text=[self getCityName:row componet:component];  
  35.     lable.font=[UIFont systemFontOfSize:14];  
  36.       
  37.     return lable;  
  38. }  


项目完整工程 https://github.com/whzhaochao/IOSChinaCity
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值