json(movie).png
#import "ViewController.h"
//#import "movie.txt"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
//存放數據的數組
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
_tableView.backgroundColor = [UIColor redColor];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
//注冊 cell
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"class"];
//解析文檔
[self Movie];
}
-(void)Movie
{
//獲取文檔路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"movie.txt" ofType:nil];
//轉化為 data
NSData *data = [NSData dataWithContentsOfFile:path];
//解析
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
//創建數組
self.dataArray = [NSMutableArray arrayWithArray:array];
}
#pragma mark --- 代理方法
//分區
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;
}
//分區下的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = [self.dataArray[section] objectForKey:@"data"];
return array.count;
}
//返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"class"];
//拿到的是第一個字典(內部包含一個人數組)
//所以下邊定義數組接收
NSDictionary *dict = self.dataArray[indexPath.section];
//數組內部有字典,需要拿到
NSArray *array = [dict valueForKey:@"data"];
//拿到數組內部的字典
NSDictionary *dict1 = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [dict1 objectForKey:@"title"];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
movie.txt
[
{
"data":
[
{
"id": "185163",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2233706697.jpg",
"icon": "1-1.jpg",
"title": "速度與激情7"
},
{
"id": "178554",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2237747953.jpg",
"icon": "1-2.jpg",
"title": "復仇者聯盟2:奧創紀元"
},
{
"id": "184885",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2257944916.jpg",
"icon": "1-3.jpg",
"title": "捉妖記"
},
{
"id": "179232",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2253038483.jpg",
"icon": "1-4.jpg",
"title": "煎餅俠"
},
{
"id": "184884",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2257475080.jpg",
"icon": "1-5.jpg",
"title": "王朝的女人·楊貴妃"
},
{
"id": "185107",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2246885606.jpg",
"icon": "1-6.jpg",
"title": "殺破狼2"
},
{
"id": "184895",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2239530046.jpg",
"icon": "1-7.jpg",
"title": "念念"
},
{
"id": "185212",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2257443441.jpg",
"icon": "1-8.jpg",
"title": "謎城"
},
{
"id": "166715",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2251450614.jpg",
"icon": "1-9.jpg",
"title": "道士下山"
},
{
"id": "184407",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2244110255.jpg",
"icon": "1-10.jpg",
"title": "橫沖直撞好萊塢"
},
{
"id": "185400",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2259282065.jpg",
"icon": "1-11.jpg",
"title": "破風"
},
{
"id": "185346",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2250399288.jpg",
"icon": "1-12.jpg",
"title": "小時代4:靈魂盡頭"
},
{
"id": "184239",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2238949728.jpg",
"icon": "1-13.jpg",
"title": "左耳"
},
{
"id": "107286",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2253155703.jpg",
"icon": "1-14.jpg",
"title": "梔子花開"
},
{
"id": "184195",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2232079769.jpg",
"icon": "1-15.jpg",
"title": "咱們結婚吧"
}
],
"title": "熱門電影"
},
{
"data":
[
{
"id": "184906",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2266591722.jpg",
"icon": "2-1.jpg",
"title": "挑戰者聯盟"
},
{
"id": "184508",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2257436776.jpg",
"icon": "2-2.jpg",
"title": "偶像來了"
},
{
"id": "184244",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2249066690.jpg",
"icon": "2-3.jpg",
"title": "爸爸去哪兒第三季"
},
{
"id": "185018",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2250415721.jpg",
"icon": "2-4.jpg",
"title": "中國好聲音 第4季"
},
{
"id": "185025",
"img": "http://img3.douban.com/view/movie_poster_cover/lpst/public/p2257441314.jpg",
"icon": "2-5.jpg",
"title": "蒙面歌王"
},
{
"id": "183980",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2244976758.jpg",
"icon": "2-6.jpg",
"title": "極限挑戰"
},
{
"id": "43161",
"img": "http://img4.douban.com/view/photo/thumb/public/p2249051446.jpg",
"icon": "2-7.jpg",
"title": "真心英雄"
},
{
"id": "175640",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2261003217.jpg",
"icon": "2-8.jpg",
"title": "變形計"
},
{
"id": "180129",
"img": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2231248476.jpg",
"icon": "2-9.jpg",
"title": "金星脫口秀"
}
],
"title": "熱門綜藝"
}
]