您的位置:首页 > 产品设计 > UI/UE

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

2015-08-29 10:23 435 查看
还记得之前写的中国省市区么?

现在我们使用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];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: