UITableView省市区数组

来源:互联网 发布:2015年程序员考试真题 编辑:程序博客网 时间:2024/04/27 06:18
////  MainViewController.m//  UI08_tableview省市区字典数组////  Created by dllo on 15/8/7.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)NSMutableArray *proArr;@end@implementation MainViewController- (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/Clare/OC/OC_省市区字典数组/OC_省市区字典数组/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 if ([temp hasPrefix:@"    "]) {            NSMutableDictionary *proDic = [self.proArr lastObject];            NSMutableArray *cityArr = proDic[@"cityArr"];            NSMutableDictionary *cityDic = [cityArr lastObject];            NSMutableArray *zoneArr = cityDic[@"zoneArr"];            [zoneArr addObject:temp];        }                }}- (void)dealloc{    [_proArr release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent = NO;        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];    self.title = @"省";    tableView.delegate = self;    tableView.dataSource = self;    [self.view addSubview:tableView];    [tableView release];                    // 读出plist文件    NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];    NSLog(@"%@", dic);}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return  self.proArr.count;}- (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] autorelease];    }    // 省字典    NSMutableDictionary *proDic = self.proArr[indexPath.row];    cell.textLabel.text = proDic[@"proName"];    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    SecondViewController *secVC = [[SecondViewController alloc] init];    [self.navigationController pushViewController:secVC animated:YES];    // 省字典    NSMutableDictionary *proDic = self.proArr[indexPath.row];    // 省字典对应的市数组    NSMutableArray *cityArr = proDic[@"cityArr"];    secVC.cityArr = cityArr;    //[secVC release];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end
////  SecondViewController.h//  UI08_tableview省市区字典数组////  Created by dllo on 15/8/7.//  Copyright (c) 2015年 Clare. All rights reserved.//#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController@property(nonatomic, retain)NSMutableArray *cityArr;@end

////  SecondViewController.m//  UI08_tableview省市区字典数组////  Created by dllo on 15/8/7.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "SecondViewController.h"#import "ThirdViewController.h"@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>@end@implementation SecondViewController- (void)dealloc{    [_cityArr release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];    self.navigationController.navigationBar.translucent = NO;    self.title = @"市";    UITableView *cityTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 64) style:UITableViewStylePlain];    [self.view addSubview:cityTabelView];    cityTabelView.dataSource = self;    cityTabelView.delegate = self;    [cityTabelView release];    }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return  self.cityArr.count;}- (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] autorelease];    }    NSMutableDictionary *cityDic = self.cityArr[indexPath.row];    cell.textLabel.text = cityDic[@"cityName"];    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];    [self.navigationController pushViewController:thirdVC animated:YES];        NSMutableDictionary *cityDic = self.cityArr[indexPath.row];    NSMutableArray *zoneArr = cityDic[@"zoneArr"];    thirdVC.zoneArr = zoneArr; }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

////  ThirdViewController.h//  UI08_tableview省市区字典数组////  Created by dllo on 15/8/7.//  Copyright (c) 2015年 Clare. All rights reserved.//#import <UIKit/UIKit.h>@interface ThirdViewController : UIViewController@property(nonatomic, retain)NSMutableArray *zoneArr;@end

////  ThirdViewController.m//  UI08_tableview省市区字典数组////  Created by dllo on 15/8/7.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "ThirdViewController.h"@interface ThirdViewController ()<UITableViewDataSource, UITableViewDelegate>@end@implementation ThirdViewController- (void)dealloc{    [_zoneArr release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];    self.navigationController.navigationBar.translucent = NO;    self.title = @"区";        UITableView *zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];    [self.view addSubview:zoneTableView];    [zoneTableView release];    zoneTableView.delegate = self;    zoneTableView.dataSource = self;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.zoneArr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static 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)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end


0 0
原创粉丝点击