将一个省市区字典数组添加到tableView中, 实现如下效果 (点击省, 显示对应的市, 点击市, 显示相应的区)
#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>
// 省市区数组
@property(nonatomic, retain)NSMutableArray *proArr;
@property(nonatomic, retain)NSMutableArray *cityArr;
@property(nonatomic, retain)NSMutableArray *zoneArr;
@property(nonatomic, retain)UITableView *proTableView;
@property(nonatomic, retain)UITableView *cityTableView;
@property(nonatomic, retain)UITableView *zoneTableView;
@end
@implementation MainViewController
- (void)dealloc
{
[_proTableView release];
[_cityTableView release];
[_zoneTableView release];
[_proArr release];
[_cityTableView 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)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
// 一个页面里面有三个tableview, 进行互相的联动, 点击省显示对应的市, 点击市显示对应的区
// 省tableView
self.proTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
[self.view addSubview:self.proTableView];
[_proTableView release];
self.proTableView.dataSource = self;
self.proTableView.delegate = self;
// 市tableView
self.cityTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
[self.view addSubview:self.cityTableView];
[_cityTableView release];
self.cityTableView.dataSource = self;
self.cityTableView.delegate = self;
// 区tableView
self.zoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
[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.proTableView) {
return self.proArr.count;
}else if (tableView == self.cityTableView) {
return self.cityArr.count;
}else{
return self.zoneArr.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.proTableView) {
static NSString *proReuse = @"proReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:proReuse] ;
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:proReuse] autorelease];
}
NSDictionary *proDic = self.proArr[indexPath.row];
cell.textLabel.text = proDic[@"proName"];
return cell;
} else if (tableView == self.cityTableView){
static NSString *cityReuse = @"cityReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityReuse] ;
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cityReuse]autorelease];
}
NSDictionary *cityDic = self.cityArr[indexPath.row];
cell.textLabel.text = cityDic[@"cityName"];
return cell;
}else{
static NSString *zoneReuse = @"zoneReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zoneReuse];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:zoneReuse];
}
cell.textLabel .text = self.zoneArr[indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 当前是哪一个tableView被触发方法
if (tableView == self.proTableView) {
// 先找到当前点击的是哪个省
NSMutableDictionary *proDic = self.proArr[indexPath.row];
self.cityArr = proDic[@"cityArr"];
// 对市的tableView进行reloaddata
[self.cityTableView reloadData];
}else if(tableView == self.cityTableView){
// 先找市字典
NSMutableDictionary *dic = self.cityArr[indexPath.row];
// 再找区数组
self.zoneArr = dic[@"zoneArr"];
// 对区数组刷新
[self.zoneTableView reloadData];
}
}
- (void)createData{
NSString *path = @"/Users/dlios/Desktop/UI/笔记/UI09_多种tableView/UI09_多种tableView/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:@"cityArr"];
[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:@"zoneArr"];
NSMutableDictionary *proDic = [self.proArr lastObject];
NSMutableArray *cityArr = proDic[@"cityArr"];
[cityArr addObject:cityDic];
} else{
NSMutableDictionary *proDic = [self.proArr lastObject];
NSMutableArray *cityArr = proDic[@"cityArr"];
NSMutableDictionary *cityDic = [cityArr lastObject];
NSMutableArray *zoneArr =cityDic[@"zoneArr"];
[zoneArr addObject:temp];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end