UI_UITableView省市区字典数组

这里写图片描述

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

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

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

@implementation RootViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

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

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    [self createData];
}

- (void)createData {
    NSString *path = @"/Users/dlios/Desktop/UI09_UITableView省市区字典数组/UI09_UITableView省市区字典数组/area(省市区版).txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.provinceArray = [NSMutableArray array];
    for (NSString *tempStr in strArr) {
        if (![tempStr hasPrefix:@" "]) {
            NSMutableDictionary *provinceDictionary = [NSMutableDictionary dictionary];
            [provinceDictionary setObject:tempStr forKey:@"provinceName"];
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDictionary setObject:cityArray forKey:@"cityArray"];
            [self.provinceArray addObject:provinceDictionary];

        } else if ([tempStr hasPrefix:@"  "] && ![tempStr hasPrefix:@"    "]) {
            NSMutableDictionary *cityDictionary = [NSMutableDictionary dictionary];
            [cityDictionary setObject:tempStr forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDictionary setObject:zoneArray forKey:@"zoneArray"];
            NSMutableDictionary *provinecDictionary = [self.provinceArray lastObject];
            NSMutableArray *cityArray = provinecDictionary[@"cityArray"];
            [cityArray addObject:cityDictionary];
        } else {
            NSMutableDictionary *provinceDictionary = [self.provinceArray lastObject];
            NSMutableArray *cityArray = provinceDictionary[@"cityArray"];
            NSMutableDictionary *cityDictionary = cityArray.lastObject;
            NSMutableArray *zoneArray = cityDictionary[@"zoneArray"];
            [zoneArray addObject:tempStr];
        }
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.provinceArray.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.provinceArray[indexPath.row][@"provinceName"];
    cell.detailTextLabel.text = @"2";
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];

    // 属性传值.
    secondVC.arr = self.provinceArray[indexPath.row][@"cityArray"];
}
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>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_arr release];
    [_tableView release];
    [super dealloc];
}

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

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];

    thirdVC.arr1 = self.arr[indexPath.row][@"zoneArray"];
}
ThirdViewController.h
#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@property(nonatomic, retain)NSMutableArray *arr1;

@end
ThirdViewController.m
#import "ThirdViewController.h"

@interface ThirdViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation ThirdViewController

- (void)dealloc
{
    [_arr1 release];
    [_tableView release];
    [super dealloc];
}

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

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr1.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];
    }

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

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值