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;
}
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;
}
相关文章推荐
- 用json方法解析本地数据,并显示在tableView上面
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- TabLayout下ViewPager显示Json解析数据
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- 浅谈iOS设计中用XML解析美团外卖地址信息并在tableView上显示
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- html解析json数据为表格table显示
- 解析json对象,使用的类库jsonkit,但解析完成后,再次使用解析的数据时会导致tableview崩溃
- 从接口读取数据,Json解析后显示在RecyclerView上
- 在tableview索引中显示搜索符号的方法
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 两种方式实现省市县三级联动(利用解析文本和json方式)
- Dojo之调用数据源(json、xml等)页面自动解析显示
- 实现解析数据table表最后的点击显示更多数据的代码
- 在tableview索引中显示搜索符号的方法
- 在tableview索引中显示搜索符号的方法
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 在tableview索引中显示搜索符号的方法
- 实现类似app store的tableview的显示更多
- 在tableview索引中显示搜索符号的方法