UITableView省市区字典
2015-08-08 21:00
399 查看
一.
1.定义一个省数组的属性
@property(nonatomic,retain)NSMutableArray *proArr;
2.对数组初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self=[super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
[self creatData];
}
return
self;
}
3.//模块化
-(void)creatData
{
NSString *path=@"/Users/dlios/Desktop/UI08_tableView省市区字典数组/UI08_tableView省市区字典数组/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];
}
}
for (NSMutableDictionary *prodic
in
self.proArr) {
NSLog(@"%@",prodic[@"proName"]);
NSMutableArray *cityArr=prodic[@"cityArr"];
for (NSMutableDictionary *citydic
in cityArr) {
NSLog(@"%@",citydic[@"cityName"]);
NSMutableArray *zoneArr=citydic[@"zoneArr"];
for (NSString *name
in zoneArr) {
NSLog(@"%@",name);
}
}
}
}
4.在ViewDidLoad里写
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];
tableView.delegate=self;
tableView.dataSource=self;
[self.view
addSubview:tableView];
[tableView
release];
5.签2个协议,设置代理人
<UITableViewDataSource,UITableViewDelegate>
6.UITableViewDataSource有2个必须执行的协议方法
- (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];
}
NSMutableDictionary *proDic=self.proArr[indexPath.row];
cell.textLabel.text=proDic[@"proName"];
return cell;
}
7.UITableViewDelegate
//tableView的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
(引头文件)
LiaoNingViewController *liao=[[LiaoNingViewController
alloc]
init];
[self.navigationController
pushViewController:liao animated:YES];
[liao
release];
NSMutableDictionary *proDic=self.proArr[indexPath.row];
//省对应的市数组
NSMutableArray *cityArr=proDic[@"cityArr"];
//传市数组
liao.arr=cityArr;
}
二.创建一个市的UIViewController
1.在.h里写一条属性,用来接收传过来的市数组
@property(nonatomic,retain)NSMutableArray *arr;
2.viewDidLoad里内容同上
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.delegate=self;
tableView.dataSource=self;
[tableView
release];
2.签协议设代理人
3.实现协议方法
//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
ZoneViewController *zone=[[ZoneViewController
alloc]
init];
[self.navigationController
pushViewController:zone animated:YES];
[zone
release];
NSMutableDictionary *citydic=self.arr[indexPath.row];
zone.arr =citydic[@"zoneArr"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.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.arr[indexPath.row];
cell.textLabel.text=cityDic[@"cityName"];
return cell;
}
三.传区名,同上,在区的UIViewController里用一个数组接收传过来的区数组
(注意:引头文件,签协议,设置代理人,传递用到的都是属性传递,协议里的必须执行方法)
1.定义一个省数组的属性
@property(nonatomic,retain)NSMutableArray *proArr;
2.对数组初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self=[super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
[self creatData];
}
return
self;
}
3.//模块化
-(void)creatData
{
NSString *path=@"/Users/dlios/Desktop/UI08_tableView省市区字典数组/UI08_tableView省市区字典数组/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];
}
}
for (NSMutableDictionary *prodic
in
self.proArr) {
NSLog(@"%@",prodic[@"proName"]);
NSMutableArray *cityArr=prodic[@"cityArr"];
for (NSMutableDictionary *citydic
in cityArr) {
NSLog(@"%@",citydic[@"cityName"]);
NSMutableArray *zoneArr=citydic[@"zoneArr"];
for (NSString *name
in zoneArr) {
NSLog(@"%@",name);
}
}
}
}
4.在ViewDidLoad里写
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];
tableView.delegate=self;
tableView.dataSource=self;
[self.view
addSubview:tableView];
[tableView
release];
5.签2个协议,设置代理人
<UITableViewDataSource,UITableViewDelegate>
6.UITableViewDataSource有2个必须执行的协议方法
- (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];
}
NSMutableDictionary *proDic=self.proArr[indexPath.row];
cell.textLabel.text=proDic[@"proName"];
return cell;
}
7.UITableViewDelegate
//tableView的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
(引头文件)
LiaoNingViewController *liao=[[LiaoNingViewController
alloc]
init];
[self.navigationController
pushViewController:liao animated:YES];
[liao
release];
NSMutableDictionary *proDic=self.proArr[indexPath.row];
//省对应的市数组
NSMutableArray *cityArr=proDic[@"cityArr"];
//传市数组
liao.arr=cityArr;
}
二.创建一个市的UIViewController
1.在.h里写一条属性,用来接收传过来的市数组
@property(nonatomic,retain)NSMutableArray *arr;
2.viewDidLoad里内容同上
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.delegate=self;
tableView.dataSource=self;
[tableView
release];
2.签协议设代理人
3.实现协议方法
//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
ZoneViewController *zone=[[ZoneViewController
alloc]
init];
[self.navigationController
pushViewController:zone animated:YES];
[zone
release];
NSMutableDictionary *citydic=self.arr[indexPath.row];
zone.arr =citydic[@"zoneArr"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.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.arr[indexPath.row];
cell.textLabel.text=cityDic[@"cityName"];
return cell;
}
三.传区名,同上,在区的UIViewController里用一个数组接收传过来的区数组
(注意:引头文件,签协议,设置代理人,传递用到的都是属性传递,协议里的必须执行方法)
相关文章推荐
- UI05_Control(UIStepper加减音量/UIBlurEffect毛玻璃/UISlider滑动/UISegmentedControl小模块)
- [leetcode] Implement Queue using Stacks
- UIButton的一些常用知识点
- [leetcode 234]Implement Queue using Stacks
- UVA 10420 List of Conquests
- UIViewController的生命周期
- UIViewController
- opencv开发之MFC的.SetWindowTextA() + OnTimer(UINT_PTR nIDEvent) +cvNamedWindow 在MFC上显示
- UI中简单的猜数字游戏
- UI, 关灯游戏的实现
- Longest Consecutive Sequence
- android自定义UI模板图文详解
- UIScrollView无法滚动的解决办法
- UITextField
- android中键盘不遮挡ui界面的方法
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- 关于自定义导航条UIBarButtonItem偏移的问题
- UIButton以及UITextField
- [LeetCode]N-Queens 八皇后问题扩展(经典深层搜索)
- Reveal UI 分析工具简单使用