省市区字典数组嵌套(UI版)
2015-07-24 21:33
435 查看
// AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSString *filePath = @"/Users/dllo/Desktop/textPCA/textPCA/area-2.txt";
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *mArray = [string componentsSeparatedByString:@"\n" ];
NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" 0123456789"]; // 字符的集合
NSMutableArray *array = [NSMutableArray array]; // 空数组
NSDictionary *rootDic = [NSDictionary dictionaryWithObjectsAndKeys:array, @"array", @"地区", @"name", nil]; // 根字典
for (NSString *s in mArray) {
NSRange r = {0, 2};
NSString *s1 = [s substringWithRange:r];
NSMutableDictionary *superDic = [NSMutableDictionary dictionaryWithDictionary:rootDic];
while ([s1 isEqualToString:@" "]) { // 几个@" "决定装到第哪层的字典里面
r.location += 2;
s1 = [s substringWithRange:r];
superDic = [superDic[@"array"] lastObject]; // 得到这个地方的字典的父字典
}
NSString *name = [s stringByTrimmingCharactersInSet:cSet]; // 字符串剔除在字符集合的字符,得到城市名字的字符串
array = [NSMutableArray array]; // 新的空数组
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", array, @"array", nil];
// 生成的新的字典,地方名和一个空数组
[superDic[@"array"] addObject:dic]; // 把字典加在父字典的数组里
[dic setObject:superDic forKey:@"superDic" ]; // 找到子字典的父字典
}
MainViewController *mainViewController = [[MainViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:mainViewController];
[mainViewController release];
mainViewController.dic = rootDic;
self.window.rootViewController = navigationController;
[navigationController release];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSString *filePath = @"/Users/dllo/Desktop/textPCA/textPCA/area-2.txt";
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *mArray = [string componentsSeparatedByString:@"\n" ];
NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" 0123456789"]; // 字符的集合
NSMutableArray *array = [NSMutableArray array]; // 空数组
NSDictionary *rootDic = [NSDictionary dictionaryWithObjectsAndKeys:array, @"array", @"地区", @"name", nil]; // 根字典
for (NSString *s in mArray) {
NSRange r = {0, 2};
NSString *s1 = [s substringWithRange:r];
NSMutableDictionary *superDic = [NSMutableDictionary dictionaryWithDictionary:rootDic];
while ([s1 isEqualToString:@" "]) { // 几个@" "决定装到第哪层的字典里面
r.location += 2;
s1 = [s substringWithRange:r];
superDic = [superDic[@"array"] lastObject]; // 得到这个地方的字典的父字典
}
NSString *name = [s stringByTrimmingCharactersInSet:cSet]; // 字符串剔除在字符集合的字符,得到城市名字的字符串
array = [NSMutableArray array]; // 新的空数组
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", array, @"array", nil];
// 生成的新的字典,地方名和一个空数组
[superDic[@"array"] addObject:dic]; // 把字典加在父字典的数组里
[dic setObject:superDic forKey:@"superDic" ]; // 找到子字典的父字典
}
MainViewController *mainViewController = [[MainViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:mainViewController];
[mainViewController release];
mainViewController.dic = rootDic;
self.window.rootViewController = navigationController;
[navigationController release];
return YES;
}
#import "MainViewController.h" @interface MainViewController ()<UITableViewDelegate,UITableViewDataSource> @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; // Do any additional setup after loading the view. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; [self.view addSubview:tableView]; [tableView release]; tableView.dataSource = self; tableView.delegate = self; tableView.rowHeight = 30; self.title = self.dic[@"name"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return ((NSArray *)self.dic[@"array"]).count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse]; } cell.textLabel.text = self.dic[@"array"][indexPath.row][@"name"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (((NSArray *)self.dic[@"array"][indexPath.row][@"array"]).count) { MainViewController *anotherVC = [[MainViewController alloc] init]; [self.navigationController pushViewController:anotherVC animated:YES]; anotherVC.dic = self.dic[@"array"][indexPath.row]; [anotherVC release]; } } @end
相关文章推荐
- 转: requestAnimationFrame,Web中写动画的另一种选择
- UI设计
- iOS - UIImageView - how to handle UIImage image orientation
- hdu 5301 Buildings 5308 I Wanna Become A 24-Point Master(构造)
- 杭电1047 Integer Inquiry【无限大数和】
- HDU 5288 OO’s Sequence
- hdoj 1509 Windows Message Queue(优先队列)
- hdoj 1047 Integer Inquiry
- Integer Inquiry hdoj 1047
- C - Building a Space Station - poj 2031
- Unique Binary Search Trees II
- CodeForces#313 D. Equivalent Strings
- HDU 4069 Squiggly Sudoku
- IOS UIFont可选字体
- Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
- 最长公共子序列 (LCS,longest common subsequence problem) 动态规划
- LightOJ 1048 - Conquering Keokradong 【二分】
- Adjust color space Constrast,Brightness,Hue,Saturation in YCbCr format.
- HDU 5301 Buildings
- HDU 1047--Integer Inquiry 【大数】