Objective-C 省市区 三个tableView联动
2015-12-24 21:09
405 查看
// 所有方法都在根视图下
// 定义属性
@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;
self.view.backgroundColor = [UIColor whiteColor];
// 第一种方式: 这个设置,需要在原有基础上-64
self.navigationController.navigationBar.translucent = NO;
// 第二种方式
// 这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明效果可以把属性设置成NO,然后再设置滚动视图的坐标位置
// self.automaticallyAdjustsScrollViewInsets = NO;
// 省tableView
self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.proTableView.dataSource = self;
self.proTableView.delegate = self;
self.proTableView.tag = 1000;
[self.view addSubview:self.proTableView];
[_proTableView release];
// 市tableView
self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.cityTableView.delegate = self;
self.cityTableView.dataSource = self;
self.cityTableView.tag = 1001;
[self.view addSubview:_cityTableView];
[_cityTableView release];
// 区tableView
self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
// self.zoneTableView.backgroundColor = [UIColor greenColor];
self.zoneTableView.delegate = self;
self.zoneTableView.dataSource = self;
[self.view addSubview:_zoneTableView];
[_zoneTableView release];
// 定义一个方法,遍历省市区
[self createData];
// 省市区遍历
- (void)createData{
NSString *path = @"/Users/dllo/Desktop/资料库/07文件/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:@"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 (NSMutableDictionary *proDic in self.proArr)
{
NSLog(@"%@",proDic[@"name"]);
for (NSMutableDictionary *cityDic in proDic[@"cityArr"])
{
NSLog(@"%@",cityDic[@"cityName"]);
for (NSString *temp in cityDic[@"zoneArr"]) {
NSLog(@"%@",temp);
}
}
}
}
// 判断当前在哪个tableView
- (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;
}
}
// 判断当前在哪个tableView,显示对应的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.proTableView == tableView) {
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.proArr[indexPath.row][@"name"];
return cell;
}else if (self.cityTableView == tableView){
// 创建市tableview的cell
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
return cell;
}
else{
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.zoneArr[indexPath.row];
return cell;
}
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 需要判断点击的是哪一个tableview
if (tableView.tag == 1000) {
// 或者用属性地址:self.proTableView == tableView
self.cityArr = self.proArr[indexPath.row][@"cityArr"];
[self.cityTableView reloadData];
// 用来消除一个小bug,可以把这句话注掉试一下
self.zoneArr = NULL;
[self.zoneTableView reloadData];
}else if (tableView.tag == 1001){
self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"];
[self.zoneTableView reloadData];
}
}
// 定义属性
@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;
self.view.backgroundColor = [UIColor whiteColor];
// 第一种方式: 这个设置,需要在原有基础上-64
self.navigationController.navigationBar.translucent = NO;
// 第二种方式
// 这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明效果可以把属性设置成NO,然后再设置滚动视图的坐标位置
// self.automaticallyAdjustsScrollViewInsets = NO;
// 省tableView
self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.proTableView.dataSource = self;
self.proTableView.delegate = self;
self.proTableView.tag = 1000;
[self.view addSubview:self.proTableView];
[_proTableView release];
// 市tableView
self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.cityTableView.delegate = self;
self.cityTableView.dataSource = self;
self.cityTableView.tag = 1001;
[self.view addSubview:_cityTableView];
[_cityTableView release];
// 区tableView
self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
// self.zoneTableView.backgroundColor = [UIColor greenColor];
self.zoneTableView.delegate = self;
self.zoneTableView.dataSource = self;
[self.view addSubview:_zoneTableView];
[_zoneTableView release];
// 定义一个方法,遍历省市区
[self createData];
// 省市区遍历
- (void)createData{
NSString *path = @"/Users/dllo/Desktop/资料库/07文件/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:@"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 (NSMutableDictionary *proDic in self.proArr)
{
NSLog(@"%@",proDic[@"name"]);
for (NSMutableDictionary *cityDic in proDic[@"cityArr"])
{
NSLog(@"%@",cityDic[@"cityName"]);
for (NSString *temp in cityDic[@"zoneArr"]) {
NSLog(@"%@",temp);
}
}
}
}
// 判断当前在哪个tableView
- (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;
}
}
// 判断当前在哪个tableView,显示对应的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.proTableView == tableView) {
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.proArr[indexPath.row][@"name"];
return cell;
}else if (self.cityTableView == tableView){
// 创建市tableview的cell
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
return cell;
}
else{
NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.zoneArr[indexPath.row];
return cell;
}
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 需要判断点击的是哪一个tableview
if (tableView.tag == 1000) {
// 或者用属性地址:self.proTableView == tableView
self.cityArr = self.proArr[indexPath.row][@"cityArr"];
[self.cityTableView reloadData];
// 用来消除一个小bug,可以把这句话注掉试一下
self.zoneArr = NULL;
[self.zoneTableView reloadData];
}else if (tableView.tag == 1001){
self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"];
[self.zoneTableView reloadData];
}
}
相关文章推荐
- 【Objective-C学习记录】第二十三天
- Objective-C 集合之NSDictionary的常用方法
- Objective - C tableView自适应高度 & KVC (Key -Value-Coding)
- Objective-C tableView自定义cell
- How to call an Objective-C Method from a C Method?
- Objective - C UITableView界面传值 从前往后,从后往前
- Objective - C UITableView学习笔记
- 【转】Android自动化测试(UiAutomator)——UiObject
- java线程同步:使用Object的wait,notify,notifyAll做线程调度
- Objective--C 导航视图控制器
- Objective-C基础1-类目(Category)和延展(Extension)的区别
- Objective-C 集合之NSArray的常用方法
- Objective-C NSObject 的实现分析(2014-10-23更新)
- Objective-C Runtime 总结:消息机制 篇
- Object.ReferenceEquals
- 遍历系统的所有ObjectType和TypeIndex
- iOS开发系列--Objective-C之KVC、KVO
- Objective-c语言_内存管理2(代码)
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- Ubuntu snmpwalk 执行报错 “Unknown Object Identifier”