解析(movie.txt)cell显示

2017-06-03  本文已影响0人  浅笑嫣然9611

解析(movie.txt)cell显示

#import "ViewController.h"@interface ViewController ()@property(nonatomic,strong)UITableView *tableView;

//1.存放数据的数组(movie.txt)

@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 blueColor];

[self.view addSubview:self.tableView];

self.tableView.delegate = self;

self.tableView.dataSource = self;

//注册cell

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

//解析JSON(movie)文档

[self movie];

}

-(void)movie{

//1.获取文档路径

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"movie.txt" ofType:nil];

//2.转化

NSData *data = [NSData dataWithContentsOfFile:filePath];

//解析 数组

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:@"cell"];

//    //拿到的第一个字典(内部包含了一个数组)

NSDictionary *dic = self.dataArray[indexPath.section];

//数组内部有字典需要拿到

NSArray *array = [dic valueForKey:@"data"];

//拿到数组内部的字典

NSDictionary *dic1 = [array objectAtIndex:indexPath.row];

//字典中title复值给cell

cell.textLabel.text = [dic1 objectForKey:@"title"];

return cell;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

上一篇 下一篇

猜你喜欢

热点阅读