项目中用到地址的pickview,后台给的数据是直接给了个db文件,当时没想太多直接按db文件来弄model
#import "ShengModel.h"
#import "ChengShiModel.h"
@implementation ShengModel
- (id) initWithDictionary:(NSDictionary*) dic
{
self=[super init];
if (self)
{
self.m_id = [dic objectForKey:@"id"];
self.m_name = [dic objectForKey:@"name"];
self.m_datas = [NSMutableArray array];
// NSString *upid =[dic objectForKey:@"upid"];
NSMutableArray *cityAry = [self searchAddressInPTCCdbWithupid:[dic objectForKey:@"id"]];
for (NSDictionary *d in cityAry) {
ChengShiModel *model=[[ChengShiModel alloc] initWithDictionary:d];
[self.m_datas addObject:model];
}
//
}
return self;
}
//查找数据库
-(NSMutableArray *)searchAddressInPTCCdbWithupid:(NSString *)upid{
NSMutableArray *array = [NSMutableArray array];
//查找数据库
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ptcc" ofType:@"db"];
FMDatabase* database = [ FMDatabase databaseWithPath: resourcePath ];
if ( ![ database open ] )
{
return nil;
}
NSString *fuzzyQuerySql = [NSString stringWithFormat:@"SELECT * FROM address WHERE upid = '%@'", upid];
//
FMResultSet* resultSet = [ database executeQuery:fuzzyQuerySql];
// 逐行读取数据
while ( [ resultSet next ] )
{
NSString *name = [resultSet stringForColumn:@"name"];
NSString *blueID = [resultSet stringForColumn:@"id"];
NSString *upid = [resultSet stringForColumn:@"upid"];
// NSLog(@" name = %@, id = %@, upid = %@", name, blueID, upid);
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:name forKey:@"name"];
[dic setObject:blueID forKey:@"id"];
[dic setObject:upid forKey:@"upid"];
[array addObject:dic];
}
return array;
}
结果发现太耗时了,每次都要查表,点地址栏太快会没来得及查完显示不完全,就自己做了张plist表果然直接读取快了很多
先把db文件中的数据都读取出来,然后写进去
listArray = [NSMutableArray array];
[self searchShengAddressInTestdbWithupid:@"0"];
NSLog(@"count = %ld", listArray.count);
//开始制作plist表
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSLog(@"str = %@", documentsPath);
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"xxlAreaList.plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
//设置属性值
[usersDic setObject:listArray forKey:@"list"];
[usersDic setObject:@(listArray.count) forKey:@"total"];
[usersDic writeToFile:plistPath atomically:YES];
NSLog(@"制表成功");
全部代码
/*
此页面为测试页面,制作地址plist表
*/
#import "TestViewController.h"
#import "ShengModel.h"
@interface TestViewController ()
{
NSMutableArray *areaArray;
NSMutableArray *listArray;
}
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"生成plist";
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"开始" forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view.mas_centerX);
make.top.mas_equalTo(100);
make.height.mas_equalTo(40);
make.width.mas_equalTo(100);
}];
}
-(void)start{
NSLog(@"111");
listArray = [NSMutableArray array];
[self searchShengAddressInTestdbWithupid:@"0"];
NSLog(@"count = %ld", listArray.count);
//开始制作plist表
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSLog(@"str = %@", documentsPath);
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"xxlAreaList.plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
//设置属性值
[usersDic setObject:listArray forKey:@"list"];
[usersDic setObject:@(listArray.count) forKey:@"total"];
[usersDic writeToFile:plistPath atomically:YES];
NSLog(@"制表成功");
}
//查找省数据库
-(void)searchShengAddressInTestdbWithupid:(NSString *)upid{
areaArray = [NSMutableArray array];
//查找数据库
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ptcc" ofType:@"db"];
FMDatabase* database = [ FMDatabase databaseWithPath: resourcePath ];
if ( ![ database open ] )
{
return;
}
NSString *fuzzyQuerySql = [NSString stringWithFormat:@"SELECT * FROM address WHERE upid = '%@'", upid];
//
FMResultSet* resultSet = [ database executeQuery:fuzzyQuerySql];
// 逐行读取数据
while ( [ resultSet next ] )
{
NSString *name = [resultSet stringForColumn:@"name"];
NSString *blueID = [resultSet stringForColumn:@"id"];
NSString *upid = [resultSet stringForColumn:@"upid"];
// NSLog(@" name = %@, id = %@, upid = %@", name, blueID, upid);
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:name forKey:@"name"];
[dic setObject:blueID forKey:@"id"];
[dic setObject:upid forKey:@"upid"];
// ShengModel *pModel = [[ShengModel alloc] initWithDictionary:dic];
// [areaArray addObject:pModel];
//查找 市
NSMutableArray *cityAry = [self searchShiAddressInPTCCdbWithupid:blueID];
[dic setObject:cityAry forKey:@"cityAry"];
[listArray addObject:dic];//省列表
}
}
//查找市数据库
-(NSMutableArray *)searchShiAddressInPTCCdbWithupid:(NSString *)upid{
NSMutableArray *array = [NSMutableArray array];
//查找数据库
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ptcc" ofType:@"db"];
FMDatabase* database = [ FMDatabase databaseWithPath: resourcePath ];
if ( ![ database open ] )
{
return nil;
}
NSString *fuzzyQuerySql = [NSString stringWithFormat:@"SELECT * FROM address WHERE upid = '%@'", upid];
//
FMResultSet* resultSet = [ database executeQuery:fuzzyQuerySql];
// 逐行读取数据
while ( [ resultSet next ] )
{
NSString *name = [resultSet stringForColumn:@"name"];
NSString *blueID = [resultSet stringForColumn:@"id"];
NSString *upid = [resultSet stringForColumn:@"upid"];
// NSLog(@" name = %@, id = %@, upid = %@", name, blueID, upid);
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:name forKey:@"name"];
[dic setObject:blueID forKey:@"id"];
[dic setObject:upid forKey:@"upid"];
//查找 区
NSMutableArray *quAry = [self searchQuAddressInPTCCdbWithupid:blueID];
[dic setObject:quAry forKey:@"quAry"];
[array addObject:dic];
}
return array;
}
//查找 区
-(NSMutableArray *)searchQuAddressInPTCCdbWithupid:(NSString *)upid{
NSMutableArray *array = [NSMutableArray array];
//查找数据库
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ptcc" ofType:@"db"];
FMDatabase* database = [ FMDatabase databaseWithPath: resourcePath ];
if ( ![ database open ] )
{
return nil;
}
NSString *fuzzyQuerySql = [NSString stringWithFormat:@"SELECT * FROM address WHERE upid = '%@'", upid];
//
FMResultSet* resultSet = [ database executeQuery:fuzzyQuerySql];
// 逐行读取数据
while ( [ resultSet next ] )
{
NSString *name = [resultSet stringForColumn:@"name"];
NSString *blueID = [resultSet stringForColumn:@"id"];
NSString *upid = [resultSet stringForColumn:@"upid"];
// NSLog(@" name = %@, id = %@, upid = %@", name, blueID, upid);
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:name forKey:@"name"];
[dic setObject:blueID forKey:@"id"];
[dic setObject:upid forKey:@"upid"];
[array addObject:dic];
}
return array;
}
@end
网友评论