数据解析

2018-05-30  本文已影响0人  鉲佈奇言

ViewController.h

@property(nonatomic,strong)NSURL *img;

ViewController.m

#import "URLService.h"
#import "UIView+ShowAlert.h"
#import "News.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "detailViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
   
    NSArray *tableDataArr;//给表格赋值的数组
}
@property(nonatomic,strong)UITableView *table;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.table.delegate=self;
    self.table.rowHeight = 60;
    self.table.dataSource=self;
    [self.view addSubview:_table];
   
    [[[URLService alloc]init]getNewsWithChannel:@"头条" num:@"40" start:@"0" appKey:nil completion:^(id data,BOOL success){
        if (!success) {
            dispatch_async(dispatch_get_main_queue(), ^{
               
                [self.view showMBHudWithMessage:data hide:2.0];
            });
            return ;
        }
        tableDataArr=[data copy];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.table reloadData];
        });       
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return tableDataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=nil;
    static NSString *reuse=@"cell";
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }
    }
    //获取cell对应的News对象
    News *one=[tableDataArr objectAtIndex:indexPath.row];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
    [img  sd_setImageWithURL:
     [NSURL URLWithString:one.pic] placeholderImage:[UIImage imageNamed:@"1"]];
    [cell addSubview:img];
   
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, self.view.frame.size.width-60, 44)];
    lable.numberOfLines = 0;
    lable.text = one.title;
    [cell addSubview:lable];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    News *one=[tableDataArr objectAtIndex:indexPath.row];
    detailViewController *detail = [[detailViewController alloc]init];
    detail.weburl = one.url;
    [self.navigationController pushViewController:detail animated:YES];
 
}

detailViewController.h

@property (nonatomic,strong)NSString *weburl;

detailViewController.m

  UIWebView *wv = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.weburl]]];
    [self.view addSubview:wv];}

News.h

@property(nonatomic,strong)NSString *title;

@property(nonatomic,strong)NSString *time;

@property(nonatomic,strong)NSString *src;

@property(nonatomic,strong)NSString *category;

@property(nonatomic,strong)NSString *pic;

@property(nonatomic,strong)NSString *content;

@property(nonatomic,strong)NSString *url;

@property(nonatomic,strong)NSString *weburl;

上一篇下一篇

猜你喜欢

热点阅读