码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI08_tableView省市区字典数组

时间:2015-08-10 09:30:25      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:ui   字典   tableview   

北京 1
  北京市 1
    西城区 1
    东城区 2
    崇文区 3
    宣武区 4
    朝阳区 5
    丰台区 6
    石景山区 7
    海淀区 8
    门头沟区 9
    房山区 10
    通州区 11
    顺义区 12
    昌平区 13
    大兴区 14
    怀柔区 15
    平谷区 16
    密云区 17
    延庆区 18
天津 2
  天津市 2
    和平区 19
    河东区 20
    河西区 21
    南开区 22
    河北区 23
    红桥区......
//
//  MainViewController.m
//  UI08_tableView省市区字典数组
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"
#import "CityViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@end

@implementation MainViewController
-(void)dealloc
{
    [_proArr release];
    [super dealloc];
}

//初始化方法
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }return self;
}
-(void)createData
{
    //文件的路径
    NSString *path=@"/Users/dllo/Desktop/作业 /UI08_tableView省市区字典数组/UI08_tableView省市区字典数组/area.txt";
    NSString *str =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr=[str componentsSeparatedByString:@"\n"];
                     self.proArr=[NSMutableArray array];
    //省市区数组
    for(NSString *temp in strArr){
        if (![temp hasPrefix:@" "]) {
            NSMutableDictionary *proDic=[NSMutableDictionary dictionary];
            [proDic setObject:temp forKey:@"proName"];
            NSMutableArray *cityArr=[NSMutableArray array];
            [proDic setObject:cityArr forKey:@"cityArr"];
            [self.proArr addObject:proDic];
        }else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "])
        {
            NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
            [cityDic setValue:temp forKey:@"cityName"];
            NSMutableArray *zoneArr=[NSMutableArray array];
            [cityDic setValue:zoneArr forKey:@"zoneArr"];
            NSMutableDictionary *proDic=[self.proArr lastObject];
            NSMutableArray *cityArr=proDic[@"cityArr"];
            [cityArr addObject:cityDic];
        }else
        {
            NSMutableDictionary *proDic=[self.proArr lastObject];
            NSMutableArray *cityArr=proDic[@"cityArr"];
            NSMutableDictionary *cityDic=[cityArr lastObject];
            NSMutableArray *zoneArr=cityDic[@"zoneArr"];
            [zoneArr addObject:temp];
        }

    }


}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    self.navigationController.navigationBar.translucent=NO;
    self.navigationItem.title=@"省";
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64) style:UITableViewStylePlain];
    tableView.dataSource=self;
    tableView.delegate=self;
    [self.view addSubview:tableView];
    [tableView release];

//    //读出plist文件内容
//    NSString *path=[[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
//    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithContentsOfFile:path];
//    NSLog(@"%@",dic);

}
//分区有多少行,和数组中元素个数一致
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.proArr.count;
}
//创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *reuse=@"reuse";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    //省字典
    NSMutableDictionary *proDic=self.proArr[indexPath.row];
    cell.textLabel.text=proDic[@"proName"];
    return cell;
}

//点击触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 省字典
    NSMutableDictionary *proDic=self.proArr[indexPath.row];
    //省对应的市数组
    NSMutableArray *cityArr=proDic[@"cityArr"];

    CityViewController *cityVC=[[CityViewController alloc] init];
    cityVC.cityArr=cityArr;
    [self.navigationController pushViewController:cityVC animated:YES ];
    [cityVC release];


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  CityViewController.h
//  UI08_tableView省市区字典数组
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CityViewController : UIViewController
@property(nonatomic,retain)NSArray *cityArr;

@end
//
//  CityViewController.m
//  UI08_tableView省市区字典数组
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "CityViewController.h"
#import "ZoneViewController.h"
@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    self.navigationController.navigationBar.translucent=NO;
    self.navigationItem.title=@"市";

    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    tableView.dataSource=self;
    tableView.delegate=self;
    [self.view addSubview:tableView];
//    [tableView release];




}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cityArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse=@"reuse";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:reuse];

    }
    NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
    cell.textLabel.text=cityDic[@"cityName"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //市字典
    NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
    NSMutableArray *zoneArr=cityDic[@"zoneArr"];

    ZoneViewController *zoneVC=[[ZoneViewController alloc] init];
    zoneVC.zoneArr=zoneArr;
    [self.navigationController pushViewController:zoneVC animated:YES];
//    [zoneVC release];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  ZoneViewController.h
//  UI08_tableView省市区字典数组
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ZoneViewController : UIViewController
@property(nonatomic,retain)NSArray *zoneArr;
@end
//
//  ZoneViewController.m
//  UI08_tableView省市区字典数组
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "ZoneViewController.h"

@interface ZoneViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ZoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor orangeColor];
    self.navigationController.navigationBar.translucent=NO;
    self.navigationItem.title=@"区";

    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    tableView.dataSource=self;
    tableView.delegate=self;
    [self.view addSubview:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.zoneArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse=@"reuse";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] ;
}

    cell.textLabel.text=self.zoneArr[indexPath.row];
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS UI08_tableView省市区字典数组

标签:ui   字典   tableview   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47393211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!