您的位置:首页 > Web前端 > JavaScript

JSON解析及省市区在tableView上的显示

2014-07-27 19:01 267 查看
将area.json解析
area.json 是一个数组套字典的形式 ,将它解析添加到tableView 上的两种形式。
1. section是省,cell是市。
2. cell是省,推出下一个页面的cell是市。

分析: 首先先将province,和cityList封装成一个省(Province)类的两个属性,通过将一个省的对象传到下一个页面的形式,来获得市的信息。

[
{

"province":
"河北省",

"cityList": [

"石家庄市",

"唐山市",

"秦皇岛市",

"邯郸市",

"邢台市",

"保定市",

"张家口市",

"承德市",

"沧州市",

"廊坊市",

"衡水市"
]
},
{

"province":
"山西省",

"cityList": [

"太原市",

"大同市",

"阳泉市",

"长治市",

"晋城市",

"朔州市",

"晋中市",

"运城市",

"忻州市",

"临汾市",

"吕梁市"
]
}

MainViewController

- (void)viewDidLoad
{

[super
viewDidLoad];

// Do any additional setup after loading the view.

// JSON解析

// 1.获取数据

NSString *path = [[NSBundle
mainBundle] pathForResource:@"area"
ofType:@"json"];

NSData *data = [NSData
dataWithContentsOfFile:path];

//
解析JSON

NSError *error = nil;

id object = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:&error];

//
输出JSON里的数据
是一个数组套字典的形式

//
遍历数组

for (NSDictionary *dic
in object) {

Province *pro = [[Province
alloc] init];
pro.name = [dic
objectForKey:@"province"];
pro.cityList = [dic
objectForKey:@"cityList"];
[self.arr
addObject:pro];
[pro
release];
}

UITableView *tableView = [[UITableView
alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
tableView.delegate =
self;
tableView.dataSource =
self;

[tableView registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"1"];
[self.view
addSubview:tableView];
[tableView
release];

}

// 省的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return [self.arr
count];

}

// section头显示的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
0, 0,
0)];
label.backgroundColor = [UIColor
purpleColor];
label.text = [[self.arr
objectAtIndex:section]
name];

return [label autorelease];
}

// 市的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// return [self.arr count];

return [[[self.arr
objectAtIndex:section]
cityList] count];
}

// cell里显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"1"];
cell.textLabel.text = [[[self.arr
objectAtIndex:indexPath.section]
cityList] objectAtIndex:indexPath.row];

return cell;
}

// 推出下一个页面时的操作。 cell里显示的是省的信息

cell的显示
省的信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
cell.textLabel.text = [[self.arr objectAtIndex:indexPath.row] name];

return cell;
}

推出第二个页面的内容
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *second = [[SecondViewController alloc] init];

// 将省的对象传过去
second.province = [self.arr objectAtIndex:indexPath.row];
[self.navigationController pushViewController:second animated:YES];
[second release];
}

SecondViewController.m
首先要见一个属性为Province类的一个对象。

// 市的个数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.province.cityList
count];

NSLog(@"city ====== %@",
self.province.cityList);
}

// cell里显示的是市的信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"1"];
cell.textLabel.text = [self.province.cityList
objectAtIndex:indexPath.row];

return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐