1.案例簡介
通過讀取文件,將中國所有城市寫入sqlite數據庫中,現通過UIPickView實現中國所有城市的選擇,效果圖如下所示
2.城市對象模型
//
// CityModel.h
// readData
//
// Created by 趙超 on 14-8-28.
// Copyright (c) 2014年 趙超. All rights reserved.
//
#import
@interface CityModel : NSObject
@property (nonatomic,copy) NSString *pid; //父級城市ID
@property (nonatomic,copy) NSString *cityName; //城市名
@property (nonatomic,copy) NSString *ids; //城市ID
@end
3.城市數據庫操作對象
sqlite封操作BseDB類請看,然後封裝城市對象的數據庫操作對象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{
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;
}