数据解析实例

2016-06-25  本文已影响6人  艾克12138

#import "Moview.h"

@implementation Moview

//******KVC赋值放崩溃,以及转换大小写,防止于系统关键字冲突*************

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

if ([key isEqualToString:@"id"]) {

self.ID =value;

}

}

@end

#import "fiveViewController.h"

#define KCELL @"cell"

#import "Moview.h"

@interface fiveViewController ()

@property(nonatomic,strong)UITableView *tableview;

@property(nonatomic,strong)NSMutableArray *array;

@property(nonatomic,strong)NSMutableArray *movieArray;

@property(nonatomic,strong)NSMutableArray *varietyArray;

- (IBAction)segment:(UISegmentedControl *)sender;

@end

@implementation fiveViewController

-(NSMutableArray *)movieArray{

if (!_movieArray) {

_movieArray =[NSMutableArray array];

}return _movieArray;

}

-(NSMutableArray *)varietyArray{

if (!_varietyArray) {

_varietyArray =[NSMutableArray array];

}return _varietyArray;

}

- (IBAction)segment:(UISegmentedControl *)sender {

if (sender.selectedSegmentIndex ==0) {

self.array =self.movieArray;

}else if (sender.selectedSegmentIndex ==1){

self.array=self.varietyArray;

}

[self.tableview reloadData];

}

-(UITableView *)tableview{

if (!_tableview) {

_tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

}return _tableview;

}

- (void)viewDidLoad {

[super viewDidLoad];

//    UISegmentedControl *segment =[[UISegmentedControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-50,  0, 100, 64)];

//    segment.backgroundColor =[UIColor redColor];

[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELL];

self.tableview.dataSource =self;

self.tableview.delegate=self;

[self.view addSubview:self.tableview];

//    [self.view addSubview:segment];

[self startPaser];

self.array =self.movieArray;

NSLog(@"%@",self.array);

// Do any additional setup after loading the view.

}

#pragma mark  ==*********cell************

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:KCELL forIndexPath:indexPath];

//cell.textLabel.text =@"sdf";

cell.textLabel.text = [self.array[indexPath.row] title];

return cell;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.array.count;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 2;

}

//开始解析数据显示

-(void)startPaser{

NSString *filepath =[[NSBundle mainBundle]pathForResource:@"movie" ofType:@"txt"];

NSData *data =[NSData dataWithContentsOfFile:filepath];

NSArray *array =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

NSDictionary *hotmoviewdic =array[0];//热门电影

NSArray *hotmoviewarrat =hotmoviewdic[@"data"];

NSLog(@"%@",hotmoviewarrat);

for (NSDictionary *dic in hotmoviewarrat) {

Moview *movie =[[Moview alloc]init];

[movie setValuesForKeysWithDictionary:dic];

[self.movieArray addObject:movie];

}

//热门综艺

NSDictionary *hotvarietydic =array[1];

NSArray *hotvarietyarray=hotvarietydic[@"data"];

for (NSDictionary *dic in hotvarietyarray) {

Moview *movie =[[Moview alloc]init];

[movie setValuesForKeysWithDictionary:dic];

[self.varietyArray addObject:movie];

}

}

上一篇下一篇

猜你喜欢

热点阅读