// AppDelegate.m
//创建导航栏
// 创建导航
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
self.window.rootViewController = nav;
// ViewController.m
//成员变量
{
UITableView *_tableView;
NSArray *_arr;
NSDictionary *_dic;
}
//=================
// 初始化表格
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
// 设置代理
_tableView.delegate = self;
_tableView.dataSource = self;
// 添加到视图上
[self.view addSubview:_tableView];
// 设置标题
self.title = @"电影列表";
// 开始解析
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"json"];
NSString *jsonString = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//json解析
_dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"--------%@",_dic);
//================
// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dic.allKeys.count;
}
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
_arr = [_dic objectForKey:[_dic.allKeys objectAtIndex:section]];
return _arr.count;
}
// 单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"1"];
}
_arr = [_dic objectForKey:[_dic.allKeys objectAtIndex:indexPath.section]];
cell.textLabel.text = [NSString stringWithFormat:@"电影名:%@",[_arr[indexPath.row]objectForKey:@"MovieName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"演员:%@,内容:%@",[_arr[indexPath.row]objectForKey:@"MovieAuthor"],[_arr[indexPath.row]objectForKey:@"MovieContent"]];
return cell;
}
// 页眉内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_dic.allKeys objectAtIndex:section];
}
//自带json没有宏
//书写json文件 movie.json
{
"武侠": [
{
"MovieName": "战狼",
"MovieAuthor": "吴京",
"MovieContent": "犯我中华者虽远必诛"
},
{
"MovieName": "战狼2",
"MovieAuthor": "吴京",
"MovieContent": "你的背后有一个强大的祖国"
}
],
"科幻": [
{
"MovieName": "大明猩",
"MovieAuthor": "歪果仁",
"MovieContent": "猩猩当明星"
}
],
"爱情": [
{
"MovieName": "漂洋过海来看你",
"MovieAuthor": "朱亚文",
"MovieContent": "浪漫的爱情故事"
},
{
"MovieName": "爱情睡醒了",
"MovieAuthor": "唐嫣",
"MovieContent": "呃呃呃"
},
{
"MovieName": "英雄联盟",
"MovieAuthor": "流浪",
"MovieContent": "我们走我们走"
}
]
}