UI09_UITableView省市区字典数组

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.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];
    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [rootVC release];
    [naVC release];

    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#import "SecondViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *proArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self createData];

}

- (void)createData {
    NSString *path = @"/Users/dllo/Desktop/青山流云/UI/UI09/UI09_UITableView省市区字典数组/UI09_UITableView省市区字典数组/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:@"cityName"];
            [self.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:@"zoneName"];

            NSMutableDictionary *proDic = self.proArr.lastObject;
            NSMutableArray *cityArr = proDic[@"cityName"];
            [cityArr addObject:cityDic];
        } else {
            NSMutableDictionary *proDic = self.proArr.lastObject;
            NSMutableArray *cityArr = proDic[@"cityName"];
            NSMutableDictionary *cityDic = cityArr.lastObject;
            NSMutableArray *zoneArr = cityDic[@"zoneName"];
            [zoneArr addObject:temp];
        }
    }
//    NSLog(@"%@", self.proArr);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
    //  传值
    secondVC.cityArr = self.proArr[indexPath.row][@"cityName"];
}

- (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];
    }
    //
    cell.textLabel.text = self.proArr[indexPath.row][@"proName"];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.proArr.count;
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property(nonatomic, retain)NSMutableArray *cityArr;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "ThirdViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation SecondViewController
- (void)dealloc
{
    [_cityArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.backgroundColor = [UIColor lightGrayColor];

    self.tableView.dataSource =self;
    self.tableView.delegate = self;


//    NSLog(@"%@", self.cityArr);

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];
    //  传值
    thirdVC.zoneArr = self.cityArr[indexPath.row][@"zoneName"];
}

- (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];
    }
    cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cityArr.count;
}

ThirdViewController.h

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@property(nonatomic, retain)NSMutableArray *zoneArr;

@end

ThirdViewController.m

#import "ThirdViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ThirdViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation ThirdViewController
- (void)dealloc
{
    [_zoneArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.view.backgroundColor = [UIColor lightGrayColor];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

}
- (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];
    }
    //  直接保存的区名
    cell.textLabel.text = self.zoneArr[indexPath.row];
    return  cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.zoneArr.count;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值