//// MainViewController.m// UI08_tableView省市区字典数组//// Created by dllo on 15/8/7.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "MainViewController.h"#import "CityViewController.h"@interfaceMainViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)NSMutableArray *proArr;
@end@implementationMainViewController
-(void)dealloc
{
[_proArr release];
[super dealloc];
}
//初始化方法
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self createData];
}returnself;
}
-(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];
}elseif ([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
{
returnself.proArr.count;
}
//创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *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>@interfaceCityViewController : 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"@interfaceCityViewController ()<UITableViewDataSource,UITableViewDelegate>@end@implementationCityViewController
- (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
{
returnself.cityArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *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>@interfaceZoneViewController : 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"@interfaceZoneViewController ()<UITableViewDataSource,UITableViewDelegate>@end@implementationZoneViewController
- (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
{
returnself.zoneArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *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