UI 08 tableView版中国省市区 -- 3页

来源:互联网 发布:服务器数据备份方案 编辑:程序博客网 时间:2024/06/01 07:11

还记得之前写的中国省市区么?
现在我们使用tableView将他显示出来.
里面用到了从前向后属性传值.
第一页效果图如下, 一共31个省
这里写图片描述

#import "ProViewController.h"#import "CityViewController.h"@interface ProViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSMutableArray *proArr;@end@implementation ProViewController- (void)dealloc{    [_proArr 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 学习/UI08TableView 省市区./UI08TableView 省市区./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)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent = NO;    self.view.backgroundColor = [UIColor redColor];    self.title = @"中国省名";    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];    [self.view addSubview:tableView];    [tableView release];    tableView.delegate = self;    tableView.dataSource = self;//    // 读出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{    CityViewController *cityVC = [[CityViewController alloc] init];    [self.navigationController pushViewController:cityVC animated:YES];    [cityVC release];    // 省字典    NSMutableDictionary *prodic = self.proArr[indexPath.row];    //省对应的市数组   cityVC.cityarr = prodic[@"cityarr"];}

第二页:
市

#import "CityViewController.h"#import "ZoomViewController.h"@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate>@end@implementation CityViewController- (void)dealloc{    [_cityarr release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor blueColor];   // NSLog(@"%@",self.cityarr);     self.title = @"市名";    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];    [self.view addSubview:tableView];    [tableView release];    tableView.delegate = self;    tableView.dataSource = self;}- (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:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];    }    NSMutableDictionary *citydic = self.cityarr[indexPath.row];    cell.textLabel.text = citydic[@"cityname"];    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    ZoomViewController *zoneVC = [[ZoomViewController alloc] init];    [self.navigationController pushViewController:zoneVC animated:YES];    [zoneVC release];    NSMutableDictionary *citydic = self.cityarr[indexPath.row];    zoneVC.zonearr = citydic[@"zonearr"];}

第三页:
区

#import "ZoomViewController.h"@interface ZoomViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>@property(nonatomic, retain)UIAlertView *alet;@end@implementation ZoomViewController- (void)dealloc{    [_zonearr release];    [_alet release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.     self.title = @"区名";    self.view.backgroundColor = [UIColor greenColor];    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];    [self.view addSubview:tableView];    [tableView release];    tableView.delegate = self;    tableView.dataSource = self;    tableView.rowHeight = 100;    self.alet = [[UIAlertView alloc] initWithTitle:@"要返回到市名吗?" message:nil delegate:self cancelButtonTitle:@"返回市名" otherButtonTitles:@"返回主页",@"Cancel", nil];}- (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:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];    }    cell.textLabel.text = self.zonearr[indexPath.row];    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [self.alet show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 0) {        [self.navigationController popViewControllerAnimated:YES];    }else if (buttonIndex == 1){        [self.navigationController popToRootViewControllerAnimated:YES];    }}
0 0
原创粉丝点击