將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;
}