AppDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (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];
[_window release];
MainViewController *mainVC=[[MainViewController alloc]init];
mainVC.num = 1;
NSString *path=@"/Users/dllo/Downloads/UI08_tableview省市区字典数组副本/UI08_tableview省市区字典数组/area(1).txt";
NSString *str=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *strArr=[str componentsSeparatedByString:@"\n"];
mainVC.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"];
// 把省字典添加到省数组里
[mainVC.proArr addObject:proDic];
} else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]){
// 创建一个市字典
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
// 给市字典添加键值对
// 市名
[cityDic setObject:temp forKey:@"cityName"];
// 创建一个区数组
NSMutableArray *zoneArr = [NSMutableArray array];
// 添加到市字典里
[cityDic setObject:zoneArr forKey:@"zoneArr"];
// 给市字典找一个位置
// 先找到对应的省字典
NSMutableDictionary *proDic = [mainVC.proArr lastObject];
// 再找到对应的市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
// 将市字典添加到市数组里
[cityArr addObject:cityDic];
} else {
// 先找省字典
NSMutableDictionary *proDic = [mainVC.proArr lastObject];
// 市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
// 市字典
NSMutableDictionary *cityDic = [cityArr lastObject];
// 区数组
NSMutableArray *zoneArr = cityDic[@"zoneArr"];
// 将区名添加到区数组里
[zoneArr addObject:temp];
}
}
UINavigationController *naVC=[[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController=naVC;
[mainVC release];
[naVC release];
return YES;
}
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,assign)int num;
@end
MainViewController.m
#import "MainViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];
self.navigationController.navigationBar.translucent=NO;
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];
}
-(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];
}
if (self.num == 1) {
NSMutableDictionary *proDic=self.proArr[indexPath.row];
cell.textLabel.text=proDic[@"proName"];
}
if (self.num == 2) {
NSMutableArray *arr= self.proArr;
NSMutableDictionary *cityDic=arr[indexPath.row];
cell.textLabel.text=cityDic[@"cityName"];
}
if (self.num == 3) {
cell.textLabel.text=self.proArr[indexPath.row];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.num == 1) {
MainViewController *secVC=[[MainViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.proArr = self.proArr[indexPath.row][@"cityArr"];
[secVC release];
secVC.num = self.num + 1;
}
if (self.num == 2) {
MainViewController *secVC=[[MainViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.proArr = self.proArr[indexPath.row][@"zoneArr"];
[secVC release];
secVC.num = self.num + 1;
}
if (self.num == 3) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}