解析

Model.h

@property(nonatomic,strong)NSString *pic;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *url;

LoadData.h

#import 
typedef void(^PassDataBlock)(NSMutableArray *arr);
@interface FirstLoadData : NSObject
+ (instancetype)shareLoadData;
- (void)getData:(PassDataBlock )block;
@end

.m

#import "FirstModel.h"
#import 
static FirstLoadData *ld;
@implementation FirstLoadData
+ (instancetype)shareLoadData
{
    if (!ld) {
        ld = [[FirstLoadData 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) {
//            FirstModel *m = [[FirstModel alloc]init];
//            m.title = dictionary[@"title"];
//            m.pic = dictionary[@"pic"];
//            m.url = dictionary[@"url"];
             FirstModel *yaowen = [FirstModel yy_modelWithDictionary:dictionary];
            [mArr addObject:yaowen];
            NSLog(@"%@",mArr);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            block(mArr);
        });
    }];
    [task resume];
}
@end

UIViewController.m

#import "FirstViewController.h"
#import "ImageScrollView.h"
#import "TableViewCell.h"
#import "FirstLoadData.h"
#import "FirstModel.h"
#import "UIImageView+WebCache.h"
#import "NextViewController.h"

@interface FirstViewController ()
{
   NSMutableArray *marr;
   MJRefreshNormalHeader *_header;
}
@property(nonatomic,strong)UITableView *tableView;;

@end

@implementation FirstViewController

-(UITableView *)tableView{
   if (!_tableView) {
       _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, FIT_Y(260), self.view.frame.size.width,SCREEN_H-FIT_Y(230)) style:UITableViewStylePlain];
       _tableView.delegate = self;
       _tableView.dataSource = self;
       _tableView.rowHeight = 290;
       _tableView.backgroundColor = [UIColor clearColor];

   }
   return _tableView;
}
- (void)ShuaXin
{
   // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
   _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
   
   // 设置自动切换透明度(在导航栏下面自动隐藏)
   _header.automaticallyChangeAlpha = YES;
   
   // 隐藏时间
   _header.lastUpdatedTimeLabel.hidden = YES;
   
   
   // 设置header
   self.tableView.mj_header = _header;
   
}
- (void)loadNewData
{
   [self viewWillAppear:YES];
   [self.tableView reloadData];
   [_header  endRefreshing];
}
- (void)viewDidLoad {
   [super viewDidLoad];
   [self.view addSubview:self.tableView];
   [self ShuaXin];
   [[FirstLoadData shareLoadData]getData:^(NSMutableArray *arr) {
       marr = arr;
       [_tableView reloadData];
   }]; 
}
#pragma mark ---UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return marr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *iden = @"cell";
   TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
   if (cell == nil) {
       cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden];
   }
   FirstModel *m = marr[indexPath.row];
   cell.title.text = m.title;
   
   [cell.image sd_setImageWithURL:[NSURL URLWithString:m.pic] placeholderImage:[UIImage imageNamed:@"103.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
       
   }];
  
   return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   FirstModel *m = marr[indexPath.row];
   NextViewController *dvc = [[NextViewController alloc]init];
   dvc.weburl = m.url;
   [self.navigationController pushViewController:dvc animated:YES];
}




@end

你可能感兴趣的:(解析)