省市区字典数组嵌套(UI版)

来源:互联网 发布:js在线客服插件 编辑:程序博客网 时间:2024/06/06 21:39

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

#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




0 0