一个ViewController里有三个TableView,分别为省市区,逐级跳转
2015-08-12 21:58
411 查看
准备工作不赘述
MainViewController.m
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]; } }
相关文章推荐
- UI05 _ 分段控件 -- UISegmentedControl
- Count Complete Tree Nodes(****)
- Java IO 总结
- UIImageView+GestureRecognizer
- 图的点连通度边连通度总结
- hdu2037
- 2015第33周三
- IT定理:摩尔定理,安迪-比尔定理,反摩尔定理
- Java多线程(4)----线程的四种状态
- Beginning SDL 2.0(2) TwinklebearDev SDL 2.0 Tutorial
- Bit Manipulation - Power of Two
- redo和undo的区别
- twisted.http 异步访问站点
- Spring入门教程(一)IOC 基本概念
- Android -- MediaRecord
- [Leetcode] Median of Two Sorted Arrays
- Servlet主要的作用
- makefile中的patsubst
- Laravel 5.1.4 + Bootstrap 3.3.5 笔记四:Laravel 控制器
- spring版本区别