您的位置:首页 > 其它

一个ViewController里有三个TableView,分别为省市区,逐级跳转

2015-08-12 21:58 411 查看
准备工作不赘述

MainViewController.m

#import "MainViewController.h"
#import "SecondViewController.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
{
[_proArr release];
[_cityArr release];
[_zoneArr release];
[_proTableView 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)createData
{
NSString *path=@"/Users/dllo/Desktop/UINote/UI 8-TableView省市区字典数组/UI 8-TableView省市区字典数组/area(1).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"];
// 创建一个市数组
self.cityArr=[NSMutableArray array];
// 将市数组添加到省字典中
[proDic setObject:_cityArr forKey:@"cityArr"];
// 将省字典放到省数组中r
[_proArr addObject:proDic];
}
else if ([temp hasPrefix:@"  "]&&![temp hasPrefix:@"    "])
{
// 找到对应的城市
// 创建一个市字典
NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
// 将市名放到市字典中
[cityDic setObject:temp forKey:@"cityName"];
// 创建一个区数组
self.zoneArr=[NSMutableArray array];
// 将区数组添加到市字典中
[cityDic setObject:_zoneArr forKey:@"zoneArr"];
// 给市字典找一个位置
// 市字典在省字典中
// 省字典在省数组中
NSMutableDictionary *proDic=[_proArr lastObject];
_cityArr=proDic[@"cityArr"];
[_cityArr addObject:cityDic];
}
else{
// 先找到省字典
NSMutableDictionary *proDic=[_proArr lastObject];
// 找到市数组
_cityArr=proDic[@"cityArr"];
// 找到市字典
NSMutableDictionary *cityDic=[_cityArr lastObject];
// 找到区数组
_zoneArr=cityDic[@"zoneArr"];
[_zoneArr addObject:temp];
}
}

}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
self.navigationController.navigationBar.translucent=NO;

// 一个页面里有三个tableView,进行互相的联动,点击省显示对应的市,点击市显示对应的区

// 铺三个tableView
_proTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT -64) style:UITableViewStyleGrouped];
[self.view addSubview:_proTableView];
_proTableView.dataSource=self;
_proTableView.delegate=self;
[_proTableView release];

_cityTableView=[[UITableView alloc]initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT-64) style:UITableViewStyleGrouped];
[self.view addSubview:_cityTableView];
_cityTableView.dataSource=self;
_cityTableView.delegate=self;
[_cityTableView release];

_zoneTableView=[[UITableView alloc]initWithFrame:CGRectMake(WIDTH /3 *2, 0, WIDTH/3, HEIGHT-64) style:UITableViewStyleGrouped];
[self.view addSubview:_zoneTableView];
_zoneTableView.dataSource=self;
_zoneTableView.delegate=self;
[_zoneTableView release];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView ==_proTableView ) {
return _proArr.count;
}
else if (tableView ==_cityTableView) {
return _cityArr.count;
}
else{
return _zoneArr.count;
}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

if (tableView==_proTableView) {
static NSString *proReuse=@"proReuse";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:proReuse];
if (!cell) {
cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:proReuse] autorelease];
}
NSMutableDictionary *proDic=_proArr[indexPath.row];
cell.textLabel.text=proDic[@"proName"];
return cell;
}
else if (tableView==_cityTableView)
{
static NSString *cityReuse=@"cityReuse";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cityReuse];
if (!cell) {
cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cityReuse]autorelease];
}
NSMutableDictionary *cityDic=_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]autorelease];
}
cell.textLabel.text=_zoneArr[indexPath.row];
return cell;
}

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 判断当前是哪一个tableView被点击
if(tableView ==_proTableView){
// 找到当前点击的省
NSMutableDictionary *dic=self.proArr[indexPath.row];
_cityArr=dic[@"cityArr"];
// 对市的tableView进行reloaddata
[_cityTableView reloadData];
}
if(tableView ==_cityTableView){
NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
_zoneArr=cityDic[@"zoneArr"];
[_zoneTableView reloadData];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: