IOS:OC--解析案例(movie)

1.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": "热门综艺"
}
]
2."ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITableView * tableView;
//存放数据的数组
@property(nonatomic,strong)NSMutableArray * dataArray;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

    self.tableView.backgroundColor=[UIColor cyanColor];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    //解析JSON(movie)wend
    [self Movie];
    }
    -(void)Movie{

    //获取文档路径
    NSString * filepath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"txt"];
    //转化为data
    NSData * fileData = [NSData dataWithContentsOfFile:filepath];
    //解析
    NSArray * array = [NSJSONSerialization JSONObjectWithData:fileData 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:@"cell"];
    //解析数据
    //拿到的是一个字典内部是一个数组
    //所以下面定义数组接收
    NSDictionary * dic = self.dataArray[indexPath.section];
    //数组内部有字典需要拿到
    NSArray * array = [dic valueForKey:@"data"];
    //拿到数组内部的字典
    NSDictionary * dic2 = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic2 objectForKey:@"title"];
    return cell;
    }

你可能感兴趣的:(IOS:OC--解析案例(movie))