iOS-解析

2018-01-19  本文已影响0人  Wang99

Model

#import <Foundation/Foundation.h>

@interface NewsModel : NSObject
@property(nonatomic,strong)NSString *pic;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *url;


@property (nonatomic,strong)NSString *time;   //时间
@property (nonatomic,strong)NSString *weixinname;  //微信名
@end

LoadData.h

#import <Foundation/Foundation.h>
typedef void(^PassDataBlock)(NSMutableArray *arr);
@interface NewsLoadData : NSObject
+ (instancetype)shareLoadData;
- (void)getData:(PassDataBlock )block;
@end

LoadData.m

#import "NewsLoadData.h"
#import "NewsModel.h"
static NewsLoadData *ld;
@implementation NewsLoadData
+(instancetype)shareLoadData{
    if (!ld) {
        ld = [[NewsLoadData alloc]init];
    }
    return ld;
}
- (void)getData:(PassDataBlock )block{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.jisuapi.com/news/get"]];
    request.HTTPMethod = @"POST";
    NSString *str = [NSString stringWithFormat:@"channel=%@&start     =%d&num=%d&appkey=%@",@"头条",0,20,@"c3b84a436b060866"];
    NSData *bodyData = [str dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = bodyData;

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = [dic objectForKey:@"result"];
        NSArray *arr = [dict objectForKey:@"list"];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dictionary in arr) {
            NewsModel *new = [NewsModel yy_modelWithDictionary:dictionary];
            [mArr addObject:new];
            NSLog(@"%@",mArr);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            block(mArr);
        });
    }];
    [task resume];
}

@end

主控制器

self.table = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.table.dataSource = self;
    self.table.delegate = self;
    self.table.rowHeight = 150;
    [self.view addSubview:self.table];
    [self ShuaXin];
    [[NewsLoadData shareLoadData]getData:^(NSMutableArray *arr) {
        _marr = arr;
        [self.table reloadData];
    }];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return marr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cellID";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NewsModel *m = marr[indexPath.row];
    cell.title.text = m.title;

    [cell.image sd_setImageWithURL:[NSURL URLWithString:m.pic] placeholderImage:[UIImage imageNamed:@"01.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

    }];

跳转下一个界面

WKWebView *wv = [[WKWebView alloc]initWithFrame:self.view.bounds];
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.weburl]]];
    [self.view addSubview:wv];
上一篇 下一篇

猜你喜欢

热点阅读