多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > IOS UIPickView+sqlite 選擇中國所有城市案例

IOS UIPickView+sqlite 選擇中國所有城市案例

來源:程序員人生   發布時間:2014-09-06 23:36:14 閱讀次數:3926次

1.案例簡介

通過讀取文件,將中國所有城市寫入sqlite數據庫中,現通過UIPickView實現中國所有城市的選擇,效果圖如下所示

2.城市對象模型

中國所有城市數據請看http://blog.csdn.net/whzhaochao/article/details/37969145,城市模型對象如下
// // 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.城市數據庫操作對象

sqlite封操作BseDB類請看http://blog.csdn.net/whzhaochao/article/details/38865535,然后封裝城市對象的數據庫操作對象CityDB
CityDB.h文件
// // 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.城市數據處理

中國城市數據放在china.txt中,需要處理后寫入數據庫中,讀取文件裝數據寫入數據庫代碼如下
//調用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顯示數據

MainViewControoler用戶數據的顯示,其.h文件內容如下

@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; }


項目完整工程https://github.com/whzhaochao/IOSChinaCity




生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美成人网在线综合视频 | 国产在线视频第一页 | 日本在线不卡视频 | 亚亚洲乱码一二三四区 | 成 人 免费观看网站 | 欧美疯狂做爰xx | 国产精品不卡片视频免费观看 | 手机看片日韩日韩国产在线看 | 久久亚洲国产精品五月天 | 亚洲免费网站 | 日本护士xxxx视频免费 | 免费又黄又爽又猛大片午夜 | 欧洲黄色毛片 | 婷婷久久综合 | 日本一二线不卡在线观看 | 色性综合 | 男女爱爱免费网站视频在线观看 | 老司机福利在线播放 | 亚洲国产成人久久99精品 | 国产一级一片免费播放视频 | 日本韩国一区二区三区 | 久久视频精品53在线观看 | 精品一区二区三区高清免费观看 | 国内精品久久久久影院中国 | 亚洲高清在线观看视频 | 国产精品久久久久久久 | 午夜精品久久久久久久第一页 | 欧美日韩生活片 | 成人中文字幕在线观看 | 亚洲欧美国产毛片在线 | 国产第4页 | 最近完整中文字幕1 | 亚洲成网站www久久九 | 久久久久嫩草影院精品 | 国产精品视频视频久久 | 在线欧美一区 | 伊人网久久网 | 久久久久国产精品嫩草影院 | 羞羞首页 | 国产乱码精品一区二区三上 | h视频网站在线观看 |