UI10_3个tableView的省市区联动

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [_window release];
    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [rootVC release];
    [naVC release];

    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#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];
    [_cityArr release];
    [_zoneArr release];
    [_provinceTableView 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)createData {
    NSString *path = @"/Users/dllo/Desktop/青山流云/UI/UI10/UI10_3个tableView省市区联动/UI10_3个tableView省市区联动/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 *proDic = [NSMutableDictionary dictionary];
            [proDic setObject:temp forKey:@"proName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [proDic setObject:cityArr forKey:@"cityName"];
            [self.provinceArr 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:@"zoneName"];

            NSMutableDictionary *proDic = self.provinceArr.lastObject;
            NSMutableArray *cityArr = proDic[@"cityName"];
            [cityArr addObject:cityDic];
        } else {
            NSMutableDictionary *proDic = self.provinceArr.lastObject;
            NSMutableArray *cityArr = proDic[@"cityName"];
            NSMutableDictionary *cityDic = cityArr.lastObject;
            NSMutableArray *zoneArr = cityDic[@"zoneName"];
            [zoneArr addObject:temp];
        }

    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //  automaticallyAdjustsScrollViewInsets会把第一个滚动视图的坐标起始位置设置成(0, 64), 但是它不会管后面的滚动视图, 所以想要保存半透明效果需要把这个属性设置成NO, 然后坐标起始位置都加64
    //  或者把导航栏设置成不透明, 坐标系就稳定了
    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.translucent = NO;

    self.provinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.provinceTableView];
    [self.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.view addSubview:self.cityTableView];
    [self.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.view addSubview:self.zoneTableView];
    [self.zoneTableView release];
    self.zoneTableView.dataSource = self;
    self.zoneTableView.delegate = self;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //  点击省出现市
    if (tableView == self.provinceTableView) {
        //  再次选择省时, 区消失
//        用[self.zoneArr removeAllObjects];有bug, 再次访问时, 变成了空,,改变了数据源
        self.zoneArr = nil;
        [self.zoneTableView reloadData];
        //  把省对应的市数组给属性赋值
        self.cityArr = self.provinceArr[indexPath.row] [@"cityName"];
        [self.cityTableView reloadData];
    } else if (tableView == self.cityTableView) {
        self.zoneArr = self.cityArr[indexPath.row][@"zoneName"];
        [self.zoneTableView reloadData];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Reuse = @"Reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Reuse];
    }
    if (tableView == self.provinceTableView) {
        cell.textLabel.text = self.provinceArr[indexPath.row][@"proName"];
    } else if (tableView == self.cityTableView) {
        cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
    } else {
        cell.textLabel.text = self.zoneArr[indexPath.row];
    }
    return cell;
}

- (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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值