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];
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];
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];
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;
}