一个页面创建多个tableView 相关联(省市区数组)
2015-08-12 10:21
417 查看
一个页面里面有三个tableView,进行互相的联动,点击省显示对应的市,点击市显示对应的区
1.创建三个数组的属性
代码:
@property(nonatomic,retain)NSMutableArray *proArr; @property(nonatomic ,retain)NSMutableArray *cityArr; @property(nonatomic ,retain)NSMutableArray *zoneArr; 三个tableView的属性 @property(nonatomic,retain)UITableView *proTableView; @property(nonatomic ,retain)UITableView *cityTableView; @property(nonatomic ,retain)UITableView *zoneTableView;
注意:如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadView,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器的数据部分,不创建视图
2.初始化并解析字典:
代码:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self creatData]; } return self; } -(void)creatData{ NSString *path=@"/Users/dllo/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 setValue:temp forKey:@"proName"]; NSMutableArray *cityArr=[NSMutableArray array]; [proDic setValue:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; }else if([temp hasPrefix:@" "]&&![temp hasPrefix:@" "] ){ NSMutableDictionary *cityDic=[NSMutableDictionary dictionary]; [cityDic setValue:temp forKey:@"cityName"]; NSMutableArray *zoneArr=[NSMutableArray array]; [cityDic setValue:zoneArr forKey:@"zoneArr"]; NSMutableArray *cityArr=[[self.proArr lastObject] objectForKey:@"cityArr"]; [cityArr addObject:cityDic]; }else{ NSMutableArray *zoneArr =[[[[self.proArr lastObject] objectForKey:@"cityArr"] lastObject] objectForKey:@"zoneArr"]; [zoneArr addObject:temp]; } } }
3.创建tableView
不要忘记代理人的设置和签订协议
设置背景颜色
self.view.backgroundColor=[UIColor yellowColor];
取消透明度
self.navigationController.navigationBar.translucent=NO;
省的tableView
代码:
self.proTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain]; [self.view addSubview:self.proTableView]; self.proTableView.delegate =self; self.proTableView.dataSource=self; [_proTableView release];
创建市的tableView
self.cityTableView =[[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:self.cityTableView];
[_cityTableView release];
self.cityTableView.delegate =self;
self.cityTableView.dataSource=self;
创建区的tableView
self.zoneTableView =[[UITableView alloc] initWithFrame:CGRectMake(2*self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain]; [self.view addSubview:self.zoneTableView]; [_zoneTableView release]; self.zoneTableView.delegate =self; self.zoneTableView.dataSource =self;
4.必须出发的协议(1)返回行数
代码:-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.proTableView) { NSLog(@"省,proArr.count = %ld",self.proArr.count); return self.proArr.count; } else if (tableView == self.cityTableView){ // NSLog(@"市,cityArr.count = %ld",self.cityArr.count); return self.cityArr.count; }else{ NSLog(@"区,zoneArr.count = %ld",self.zoneArr.count); return self.zoneArr.count; } }
5.必须触发的协议(2)创建cell
代码:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView == self.proTableView) { NSLog(@"sheng"); static NSString *proReuse =@"proReuse"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:proReuse]; if (!cell) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:proReuse] autorelease]; } cell.textLabel.text=self.proArr[indexPath.row][@"proName"]; return cell; }else if(tableView == self.cityTableView){ NSLog(@"shi"); static NSString *cityReuse =@"cityReuse"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cityReuse]; if (!cell) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityReuse] autorelease]; } cell.textLabel.text=self.cityArr[indexPath.row][@"cityName"]; return cell; }else{ NSLog(@"qu"); static NSString *zoneReuse =@"zoneReuse"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:zoneReuse]; if (!cell) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:zoneReuse] autorelease]; } cell.textLabel.text=self.zoneArr[indexPath.row]; return cell; } }
6,点击触发协议
代码:-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // self.clickIndexPath =indexPath; // [self.cityTableView reloadData]; // 当前是哪一个tableView被点击 if(tableView ==self.proTableView){ // 先找到是哪个省 // NSMutableDictionary *proDic =self.proArr[indexPath.row]; self.cityArr=self.proArr[indexPath.row][@"cityArr"]; //对市数组进行reloaddate [self.cityTableView reloadData]; // self.zoneArr =nil; // [self.zoneTableView reloadData]; self.zoneArr=nil; [self.zoneTableView reloadData]; }else if (tableView == self.cityTableView){ // 找市字典,再找区数组 self.zoneArr =self.cityArr[indexPath.row][@"zoneArr"]; // 对区数组进行刷新 [self.zoneTableView reloadData]; } }
相关文章推荐
- 一个页面创建多个tableView 相关联(省市区数组)
- 一个页面创建多个tableView 相关联(省市区数组)
- java注解的强大
- 社説 20150812 川内原発再稼働 電力安定供給へ重要な一歩だ
- 网页版pdf转换方法
- Kurento应用开发指南(以Kurento 5.0为模板) 之一:简介,安装与卸载
- 百度编辑器
- Celient-Script公共代码
- HTML判断IE版本,如:[if lte IE 9]……[endif]
- LNMP--Nginx的日志切割
- iOS 9应用开发教程之多行读写文本ios9文本视图
- 查看centos服务器硬盘状态信息
- iOS webView的一些用法
- android编译surface c++程序时,报错缺少.o文件
- (C#)设计模式之状态模式
- 为什么iPhone拍照更好看
- HDU 1875 畅通工程再续 (克鲁斯卡尔_最小生成树)
- 扫描二维码&集合排序
- HDU 1016 素数环