UI 08 tableView版中国省市区 -- 3页
2015-08-29 10:23
435 查看
还记得之前写的中国省市区么?
现在我们使用tableView将他显示出来.
里面用到了从前向后属性传值.
第一页效果图如下, 一共31个省
第二页:
第三页:
现在我们使用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]; } }
相关文章推荐
- UI tableView 的头视图 & "小广告"
- Win10正式版怎么删除搜狗输入法中的SogouExe顽固文件?
- UVA 1605 Building for UN
- UI 07 界面传值 之 从后向前 ---- 协议传值
- HDU 5147 Sequence II (线段树求前缀和、后缀和)
- UIView的alpha、hidden和opaque属性之间的关系和区别
- C#基础----string和stringBuilder
- Win10 10525升级Win10 Build 10532失败回滚怎么办?
- POJ 3080 Blue Jeans(水~)
- 单行文本框和多行文本框UiTextfield UiTextView
- POJ 2871 A Simple Question of Chemistry(水~)
- POJ 2785 4 Values whose Sum is 0(hash表)
- 从数据库中提取到ueditor并且重新编辑
- POJ 2664 && HDU 1144 Prerequisites?(水~)
- 【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】
- 6、iOS 开发之 UIPickerView
- 4、iOS 开发之 UITableView
- 3、iOS 开发之 UIScrollView UIPageController NSTimer
- iOS部分-UI基础控件 - 01天 入门 第11课 上下左右移动
- 新建Android工程报错:'Building workspace' has encountered a problem...解决办法