省市区3个tableView联动实现

时间:2021-08-03 11:37:31
//
// RootViewController.m
// UI10_3个UITableView省市区联动
//
// Created by dllo on 15/9/28.
// Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *provinceArr;
@property(nonatomic, retain)NSMutableArray *cityArr;
@property(nonatomic, retain)NSMutableArray *zoneArr;
@property(nonatomic, retain)UITableView *provinceTableView;
@property(nonatomic, retain)UITableView *cityTableView;
@property(nonatomic, retain)UITableView *zoneTableView;
@end

@implementation RootViewController
- (void)dealloc {
    [_provinceArr release];
    [_provinceTableView release];
    [_cityArr release];
    [_cityTableView release];
    [_zoneArr 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/ObjectiveC/OC课堂笔记/OC9_高级指针&&集合/OC9_高级指针&&集合/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.provinceArr = [NSMutableArray array];
    for (NSString *temp in strArr) {
        if (![temp hasPrefix:@" "]) {
            NSMutableDictionary *pDic = [NSMutableDictionary dictionary];
            [pDic setObject:temp forKey:@"province"];
            NSMutableArray *cArr = [NSMutableArray array];
            [pDic setObject:cArr forKey:@"city"];
            [self.provinceArr addObject:pDic];
        } else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]) {
            NSMutableDictionary *pDic = self.provinceArr.lastObject;
            NSMutableArray *cArr = pDic[@"city"];
            NSMutableDictionary *cDic = [NSMutableDictionary dictionary];
            [cDic setObject:temp forKey:@"city"];
            NSMutableArray *zArr = [NSMutableArray array];
            [cDic setObject:zArr forKey:@"zone"];
            [cArr addObject:cDic];
        } else {
            NSMutableDictionary *pDic = self.provinceArr.lastObject;
            NSMutableArray *cArr = pDic[@"city"];
            NSMutableDictionary *cDic = cArr.lastObject;
            NSMutableArray *zArr = cDic[@"zone"];
            [zArr addObject:temp];
        }
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // automaticallyAdjustsScrollViewInsets 会把第一个滚动视图的坐标起始位置设置成(0, 64), 但是不会管后面的滚动视图, 所有要想保留半透明效果需要把这个属性设置成NO,然后坐标起始位置都加64
    // 然后把导航栏设置成不透明, 坐标系就稳定了
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.provinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT) style:UITableViewStylePlain];
    self.provinceTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.provinceTableView];
    [_provinceTableView release];
    self.provinceTableView.dataSource = self;
    self.provinceTableView.delegate = self;


    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
    self.cityTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.cityTableView];
    [_cityTableView release];
    self.cityTableView.dataSource = self;
    self.cityTableView.delegate = self;


    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
    self.zoneTableView.backgroundColor = [UIColor whiteColor];
    [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.provinceTableView) {
        return self.provinceArr.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.provinceTableView) {
// static NSString *reuse = @"reuse";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// if (!cell) {
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
// }
// cell.textLabel.text = self.provinceArr[indexPath.row][@"province"];
// return cell;
// } else if (tableView == self.cityTableView) {
// static NSString *cityReuse = @"cityReuse";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityReuse];
// if (!cell) {
// cell = [[[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:cityReuse] autorelease];
// }
// cell.textLabel.text = self.cityArr[indexPath.row][@"city"];
// return cell;
// } else {
// static NSString *zoneReuse = @"zoneReuse";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zoneReuse];
// if (!cell) {
// cell = [[[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:zoneReuse] autorelease];
// }
// cell.textLabel.text = self.zoneArr[indexPath.row];
// return cell;
// }
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:reuse] autorelease];
    }
    if (tableView == self.provinceTableView) {
        cell.textLabel.text = self.provinceArr[indexPath.row][@"province"];
        return cell;
    } else if (tableView == self.cityTableView) {
        cell.textLabel.text = self.cityArr[indexPath.row][@"city"];
        return cell;
    } else {
        cell.textLabel.text = self.zoneArr[indexPath.row];
        return cell;
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 点击省出现市
    if (tableView == self.provinceTableView) {
        // 把省对应的市的数组给属性赋值
        self.cityArr= self.provinceArr[indexPath.row][@"city"];
        [self.cityTableView reloadData];
        self.zoneArr = nil;
        [self.zoneTableView reloadData];
    } else if (tableView == self.cityTableView) {
        self.zoneArr = self.cityArr[indexPath.row][@"zone"];
        [self.zoneTableView reloadData];
    } else {

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

省市区3个tableView联动实现