美文网首页
数据解析实例

数据解析实例

作者: 艾克12138 | 来源:发表于2016-06-25 20:56 被阅读6次

    #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];

    }

    }

    相关文章

      网友评论

          本文标题:数据解析实例

          本文链接:https://www.haomeiwen.com/subject/cubodttx.html