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

// 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值