三个tableView联动
2015-12-22 19:46
239 查看
#import "RootViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,retain)UITableView *proTableView; @property(nonatomic,retain)UITableView *cityTableView; @property(nonatomic,retain)UITableView *zoneTableView; @property(nonatomic,retain)NSMutableArray *proArr; @property(nonatomic,retain)NSMutableArray *cityArr; @property(nonatomic,retain)NSMutableArray *zoneArr; @end @implementation RootViewController - (void)dealloc { [_proTableView release]; [_cityTableView release]; [_zoneTableView release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; //这个设置,需要在高度的基础上-64 self.navigationController.navigationBar.translucent = NO; self.automaticallyAdjustsScrollViewInsets = NO; //这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是像保留半透明效果就可以把属性设置成NO, self.proArr = [NSMutableArray array]; self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT) style:UITableViewStylePlain]; [self.view addSubview:_proTableView]; [_proTableView release]; self.proTableView.tag = 1000; self.proTableView.dataSource = self; self.proTableView.delegate = self; self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT-64) style:UITableViewStylePlain]; [self.view addSubview:_cityTableView]; [_cityTableView release]; self.cityTableView.tag = 1001; self.cityTableView.dataSource = self; self.cityTableView.delegate = self; self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH*2/3, 0, WIDTH/3, HEIGHT-64) style:UITableViewStylePlain]; [self.view addSubview:_zoneTableView]; [_zoneTableView release]; self.zoneTableView.tag = 1002; self.zoneTableView.dataSource = self; self.zoneTableView.delegate = self; [self createData]; } -(void)createData{ NSString *path = @"/Users/dllo/Desktop/UI11_三个tableView联动/UI11_三个tableView联动/area.txt"; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSArray *strArr = [str componentsSeparatedByString:@"\n"]; for (NSString *temp in strArr) { if (![temp hasPrefix:@" "]) { NSMutableDictionary *proDic = [NSMutableDictionary dictionary]; [proDic setObject:temp forKey:@"name"]; 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]; } } // for (NSDictionary *dic in self.proArr) { // NSLog(@"%@",dic[@"name"]); // NSArray *cityArr = dic[@"cityArr"]; // for (NSDictionary *dic in cityArr) { // NSLog(@"%@",dic[@"cityName"]); // NSArray *zoneArr = dic[@"zoneArr"]; // for (NSString *str in zoneArr) { // NSLog(@"%@",str); // } // } // } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.proTableView == tableView) { return self.proArr.count; } else if (self.cityTableView == tableView){ return self.cityArr.count; } else{ return self.zoneArr.count; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.proTableView == tableView){ static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } NSDictionary *dic = self.proArr[indexPath.row]; cell.textLabel.text = dic[@"name"]; return cell; }else if (self.cityTableView == tableView){ //创建市tableview的cell static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } NSDictionary *dic = self.cityArr[indexPath.row]; cell.textLabel.text = dic[@"cityName"]; return cell; }else{ static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } cell.textLabel.text = self.zoneArr[indexPath.row]; return cell; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //需要判断点击的是哪一个tableview // if (self.proTableView == tableView) //1种是通过tag值,和协议方法的tableview对象进行比较 //2种是空间写成属性,然后判断地址是否相同 if (self.proTableView == tableView) { self.cityArr = self.proArr[indexPath.row][@"cityArr"]; [self.cityTableView reloadData]; } else if (self.cityTableView == tableView) { self.zoneArr = self.cityArr [indexPath.row][@"zoneArr"]; [self.zoneTableView reloadData]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 闭包闭包!!!
- Java常见面试题
- Naive Pattern Searching
- 设计模式:接口隔离原则
- 思杰 Citrix xendesktop 7.6 企业级布署 Citrix License server 安装
- 思杰 Citrix xendesktop 7.6 企业级布署 Citrix License server 安装
- RMQ问题 (st+线段树)
- Android自学笔记-5-click事件的四种写法
- Hive Shell命令之一(数据库和表的操作)
- GDB+GdbServer: ARM程序调试
- Qt 操作字符串型的xml
- 使用kettle制作拉链表
- HNU OJ10320 穿越火线 简单模拟
- android下调试声卡驱动之概述
- JVM分代垃圾回收策略的基础概念
- 经济--分级基金3
- CentOS下安装LNMP环境
- poj 1308 Is It A Tree? 数据结构 并查集
- 谈谈KMP算法
- POJ 2559 Largest Rectangle in a Histogram