UI09_多种tableView
来源:互联网 发布:python 迭代器使用 编辑:程序博客网 时间:2024/04/30 08:41
将一个省市区字典数组添加到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
0 0
- UI09_多种tableView
- iOS UI09_多种Tableview
- UI09_多种TableView
- UI09_多种UITableView(一个界面出现三个tableView,省市区数组)
- 多种tableview
- ios tableview多种表格形式
- UI09_自定义cell
- UI09_重写Cell
- iOS UI09_自定义cell
- UI09_自定义cell
- UI09_自定义cell
- UI09_协议控制
- 31.多种tableView和带分区的省市区显示
- 仿QQ空间用一个tableview显示多种自定义cell
- UI09_自定义cell写通讯录
- TableView
- TableView
- tableview
- 杭电 1711 Number Sequence kmp
- 数据结构(13)线性表
- 数学之最美公式
- Dragon Ball--hdoj
- Android的MediaPlayer架构介绍
- UI09_多种tableView
- 癌症晚期的妻子被丈夫威胁离婚后,居然有一位“备胎”站了出来
- mysql 创建用户
- 【笔试】15、输入三个整数x,y,z,请把这三个数由小到大输出。
- ubuntu 安装后要做的事情
- Map Labeler (poj 2296 二分+2-SAT)
- leetcode 118 —— Pascal's Triangle
- 织梦dedecms更改友情链接logo图片大小
- Linux常用命令(四)