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

來源:互聯網
上載者:User

IOS UIPickView+sqlite 選擇中國所有城市案例,uipickviewsqlite
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,然後封裝城市對象的資料庫操作對象CityDBCityDB.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 CityDBstatic 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





ios中怎操作sqlite3資料庫操作

用 FMDB 這個開源庫 ! 對sqlite3做了封裝,用起來很方便 !
 
ios中的 sqlite跟sqlserver遠端資料庫怎連結?想進行資料互動的,不用介面,

我只是過來做任務的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.