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

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里用一个数组接收传过来的区数组

(注意:引头文件,签协议,设置代理人,传递用到的都是属性传递,协议里的必须执行方法)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: