#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *provinceArr;
@property(nonatomic, retain)NSMutableArray *cityArr;
@property(nonatomic, retain)NSMutableArray *zoneArr;
@property(nonatomic, retain)UITableView *provinceTableView;
@property(nonatomic, retain)UITableView *cityTableView;
@property(nonatomic, retain)UITableView *zoneTableView;
@end
@implementation RootViewController
- (void)dealloc {
[_provinceArr release];
[_provinceTableView release];
[_cityArr release];
[_cityTableView release];
[_zoneArr release];
[_zoneTableView release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self createData];
}
return self;
}
- (void)createData {
NSString *path = @"/Users/dllo/Desktop/ObjectiveC/OC课堂笔记/OC9_高级指针&&集合/OC9_高级指针&&集合/area.txt";
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
self.provinceArr = [NSMutableArray array];
for (NSString *temp in strArr) {
if (![temp hasPrefix:@" "]) {
NSMutableDictionary *pDic = [NSMutableDictionary dictionary];
[pDic setObject:temp forKey:@"province"];
NSMutableArray *cArr = [NSMutableArray array];
[pDic setObject:cArr forKey:@"city"];
[self.provinceArr addObject:pDic];
} else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]) {
NSMutableDictionary *pDic = self.provinceArr.lastObject;
NSMutableArray *cArr = pDic[@"city"];
NSMutableDictionary *cDic = [NSMutableDictionary dictionary];
[cDic setObject:temp forKey:@"city"];
NSMutableArray *zArr = [NSMutableArray array];
[cDic setObject:zArr forKey:@"zone"];
[cArr addObject:cDic];
} else {
NSMutableDictionary *pDic = self.provinceArr.lastObject;
NSMutableArray *cArr = pDic[@"city"];
NSMutableDictionary *cDic = cArr.lastObject;
NSMutableArray *zArr = cDic[@"zone"];
[zArr addObject:temp];
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = YES;
self.provinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT) style:UITableViewStylePlain];
self.provinceTableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.provinceTableView];
[_provinceTableView release];
self.provinceTableView.dataSource = self;
self.provinceTableView.delegate = self;
self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.cityTableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.cityTableView];
[_cityTableView release];
self.cityTableView.dataSource = self;
self.cityTableView.delegate = self;
self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.zoneTableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.zoneTableView];
[_zoneTableView release];
self.zoneTableView.dataSource = self;
self.zoneTableView.delegate = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.provinceTableView) {
return self.provinceArr.count;
} else if (tableView == self.cityTableView) {
return self.cityArr.count;
} else {
return self.zoneArr.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:reuse] autorelease];
}
if (tableView == self.provinceTableView) {
cell.textLabel.text = self.provinceArr[indexPath.row][@"province"];
return cell;
} else if (tableView == self.cityTableView) {
cell.textLabel.text = self.cityArr[indexPath.row][@"city"];
return cell;
} else {
cell.textLabel.text = self.zoneArr[indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.provinceTableView) {
self.cityArr= self.provinceArr[indexPath.row][@"city"];
[self.cityTableView reloadData];
self.zoneArr = nil;
[self.zoneTableView reloadData];
} else if (tableView == self.cityTableView) {
self.zoneArr = self.cityArr[indexPath.row][@"zone"];
[self.zoneTableView reloadData];
} else {
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end