解析(movie.txt)cell顯示
#import "ViewController.h"@interface ViewController ()@property(nonatomic,strong)UITableView *tableView;
//1.存放數(shù)據(jù)的數(shù)組(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;
//注冊(cè)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.轉(zhuǎn)化
NSData *data = [NSData dataWithContentsOfFile:filePath];
//解析 數(shù)組
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//創(chuàng)建數(shù)組
self.dataArray = [NSMutableArray arrayWithArray:array];
}
#pragma mark ---代理方法
//分區(qū)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;
}
//分區(qū)下的行數(shù)
-(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"];
//? ? //拿到的第一個(gè)字典(內(nèi)部包含了一個(gè)數(shù)組)
NSDictionary *dic = self.dataArray[indexPath.section];
//數(shù)組內(nèi)部有字典需要拿到
NSArray *array = [dic valueForKey:@"data"];
//拿到數(shù)組內(nèi)部的字典
NSDictionary *dic1 = [array objectAtIndex:indexPath.row];
//字典中title復(fù)值給cell
cell.textLabel.text = [dic1 objectForKey:@"title"];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end