三个tableView分别显示省 市 区 点击省跳转相应的市,点击市跳转相应的区
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *cityArr;
@property(nonatomic,retain)NSMutableArray *zoneArr;
@end
MainViewController.m
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@end
@implementation MainViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self createData];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];
self.navigationController.navigationBar.translucent=NO;
// 铺一个tableView
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.dataSource=self;
tableView.delegate=self;
[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;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuse=@"reuse";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
}
// 先找到省字典 找到对应的省名
NSMutableDictionary *proDic=self.proArr[indexPath.row];
cell.textLabel.text=proDic[@"proName"];
return cell;
}
-(void)createData
{
NSString *path=@"/Users/dllo/Desktop/UINote/UI 8-TableView省市区字典数组/UI 8-TableView省市区字典数组/area(1).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"];
// 创建一个市数组
self.cityArr=[NSMutableArray array];
// 将市数组添加到省字典中
[proDic setObject:self.cityArr forKey:@"cityArr"];
// 将省字典放到省数组中
[self.proArr addObject:proDic];
}
else if ([temp hasPrefix:@" "]&&![temp hasPrefix:@" "])
{
// 找到对应的城市
// 创建一个市字典
NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
// 将市名放到市字典中
[cityDic setObject:temp forKey:@"cityName"];
// 创建一个区数组
self.zoneArr=[NSMutableArray array];
// 将区数组添加到市字典中
[cityDic setObject:self.zoneArr forKey:@"zoneArr"];
// 给市字典找一个位置
// 市字典在省字典中
// 省字典在省数组中
NSMutableDictionary *proDic=[self.proArr lastObject];
self.cityArr=proDic[@"cityArr"];
[self.cityArr addObject:cityDic];
}
else{
// 先找到省字典
NSMutableDictionary *proDic=[self.proArr lastObject];
// 找到市数组
self.cityArr=proDic[@"cityArr"];
// 找到市字典
NSMutableDictionary *cityDic=[self.cityArr lastObject];
// 找到区数组
self.zoneArr=cityDic[@"zoneArr"];
[self.zoneArr addObject:temp];
}
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 找到省字典
NSMutableDictionary *proDic = self.proArr[indexPath.row];
//省字典对应的市数组
self.cityArr = proDic[@"cityArr"];
SecondViewController *secVC=[[SecondViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.arr = self.cityArr;
[secVC release];
}
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *arr;
@end
SecondViewController..m
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent=NO;
UITableView *tabelView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:tabelView];
[tabelView release];
tabelView.dataSource=self;
tabelView.delegate=self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.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] autorelease];
}
// 找到对应的市名
NSMutableArray *arr= self.arr;
NSMutableDictionary *cityDic=arr[indexPath.row];
cell.textLabel.text=cityDic[@"cityName"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ThirdViewController *thiVC=[[ThirdViewController alloc]init];
[self.navigationController pushViewController:thiVC animated:YES];
// 找到区数组
NSMutableDictionary *cityDic = self.arr[indexPath.row];
NSMutableArray *arr=cityDic[@"zoneArr"];
thiVC.arr=arr;
[thiVC release];
}
ThirdViewController.h
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *arr;
@end
ThirdViewController.m
#import "ThirdViewController.h"
@interface ThirdViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent=NO;
UITableView *tabelView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64 ) style:UITableViewStylePlain];
[self.view addSubview:tabelView];
[tabelView release];
tabelView.dataSource=self;
tabelView.delegate=self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.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]autorelease];
}
// 找到区数组
// NSMutableArray *arr=self.arr;
cell.textLabel.text=self.arr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController popToRootViewControllerAnimated:YES];
}