2018-05-31

Mymessage.h:// 第四层

@interface Mylist : NSObject

@property (nonatomic,strong) NSString * title ;

@property (nonatomic,strong) NSString * time ;

@property (nonatomic,strong) NSString * pic ;

@end

// 第三层

@interface myresult : NSObject

@property (nonatomic,strong) NSString * channel ;

@property (nonatomic,strong) NSArray * list ;

@end

// 第二层

@interface MYresult : NSObject

@property (nonatomic,strong) myresult * wresult ;

@end

// 第一层

@interface Mymessage : NSObject

@property (nonatomic,strong) MYresult * qresult ;

@end

Mymessage.m:@implementation Mylist

@end

// 第三层

@implementation myresult

// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。

+ (NSDictionary *)modelContainerPropertyGenericClass {

    return @{@"list" : [Mylist class]


            };

}

@end

// 第二层

@implementation MYresult

//返回一个 Dict,将 Model 属性名对映射到 JSON 的 Key。

+ (NSDictionary *)modelCustomPropertyMapper {

    return @{@"wresult" : @"result",

            };

}

@end

// 第一层

@implementation Mymessage

//返回一个 Dict,将 Model 属性名对映射到 JSON 的 Key。

+ (NSDictionary *)modelCustomPropertyMapper {

    return @{@"qresult" : @"result",

            };

}

@end

viewcontroller.m:#import "ViewController.h"

#import "YYModel.h"

#import "Mymessage.h"

#import "MJRefresh.h"

#import "MBProgressHUD.h"

#import "UIImageView+WebCache.h"// 链接#

define URL @"https://way.jd.com/jisuapi/get?channel=头条&num=10&start=5&appkey=54e619938bc38b40151c7bc35a29067e"

@interface ViewController (){

    //初始化类

    Mymessage*  message;

    // 将解析的数据存储到数组中

  __block NSMutableArray * arr1 ,*arr2 ,*arr3;

    // 上下拉刷新

    MJRefreshFooterView *foot;

    MJRefreshHeaderView * header ;


    // 用来展示表格cell的个数

    int i ;


    // cell 中添加的图片框

    UIImageView * img ;



}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    i = 5 ;

    // 刷新数据

    [self shuaxin];

}

// 刷新显示数据

-(void)shuaxin

{

    // 尾部刷新

    foot = [MJRefreshFooterView footer];

    foot.scrollView = self.tableView;

    foot.delegate = self;

    // 头部刷新

    header = [MJRefreshHeaderView header];

    header.scrollView = self.tableView;

    header.delegate = self;

    // 开始下拉刷新  直接进入回调方法

    [header beginRefreshing];

}

// 刷新进入方法

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{



    if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {

        //2秒以后调用

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            // 10    1

            // 请求数据

            [self request];

            [self.tableView reloadData];

            // 结束

            [header endRefreshing];

        });

    }else{

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            // 上拉刷新  会出现第二页的数据

            i = arr1.count;


            [self.tableView reloadData];

            [foot endRefreshing];

        });

    }



}

-(void)request

{

    // 转换编码格式

    NSString * str = [URL stringByAddingPercentEscapesUsingEncoding:

                      NSUTF8StringEncoding];


    // 请求数据

    NSURLSession * session = [NSURLSession sharedSession];


    NSURLSessionTask * task  = [session dataTaskWithURL:[NSURL URLWithString:str] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {


        arr1 = [[NSMutableArray alloc]init];

        arr2 = [[NSMutableArray alloc]init];

        arr3 = [[NSMutableArray alloc]init];


        // yymodel  解析

        message = [Mymessage yy_modelWithJSON:data];


        // 遍历数据

        for (Mylist * list  in message.qresult.wresult.list) {

            // 加入到数组中

            [arr1 addObject:list.title];

            [arr2 addObject:list.time];

            [arr3 addObject:list.pic];


        }

        // 回到主线程刷新表格

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableView reloadData];

        });




    }];

    // 开始请求

    [task resume];


}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

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

#warning Incomplete implementation, return the number of sections

    return 1;

}

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

#warning Incomplete implementation, return the number of rows

    return i;

}

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

{

    // 解决cell 重用 标识符随 indexPath.row 变化

    NSString * str = [NSString stringWithFormat:@"cell %ld",indexPath.row];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];


    if(!cell)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];

    }

    // 添加文字

    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 160, 150)];

    lab.text = arr1[indexPath.row];

    lab.numberOfLines = 0 ;

    [cell.contentView addSubview:lab];



    // 添加时间

    UILabel * lab2 = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 160, 130, 150, 20)];

    lab2.text = arr2[indexPath.row];

    lab2.numberOfLines = 0 ;

    [cell.contentView addSubview:lab2];


    // 添加图片

    img = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 160, 0, 150, 130)];


//    img.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",arr3[indexPath.row]]]]];


    //  sdwebimage  库中的方法用来请求 网络图片

    // placeholderImage 这个参数是用来设置 还没有请求到图片之前展示的图片信息

    // 简单来说就是 一个占位符

    [img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",arr3[indexPath.row]]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

            img.image = image ;

    }];

    // 调用加载的进度方法

    [self HUD] ;


    [cell.contentView addSubview:img];



    return cell ;

}

// 正在加载的进度

-(void)HUD

{


    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:img];


    //显示hud的模式

    hud.mode = MBProgressHUDAnimationFade;



    //背景颜色

    hud.color = [UIColor grayColor];


    //主标题

    hud.labelText = @"正在加载";


    //副标题

    //    hud.detailsLabelText = @"副标题";


    //显示、隐藏时的动画样式

    hud.animationType = MBProgressHUDAnimationZoomIn;


    //当mode的属性是跟进度相关时,就可以设置progress的值,实现实时进度的显示

    hud.progress = 0.8;


    // HUD的相对于父视图 x 的偏移,默认居中

    //    hud.xOffset = 50;

    //    hud.yOffset = 50;


    //是否显示蒙板

    hud.dimBackground = YES;


    //HUD内部视图相对于HUD的内边距

    hud.margin = 50;


    //HUD的圆角半径

    //    hud.cornerRadius = 20;


    //最小的显示时间

    hud.minShowTime = 3.0;


    // HUD的最小尺寸

    hud.minSize = CGSizeMake(300, 300);


    // 代理中只有一个方法,即获得HUD隐藏后的时刻

    //    hud.delegate = self;

    // 加入到img当中

    [img addSubview:hud];


    [hud showAnimated:YES whileExecutingBlock:^{

        //hud执行期间

//        NSLog(@"执行期间");

    } onQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{

        //hud执行完毕

//        NSLog(@"执行完毕");


    }];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 150 ;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,647评论 8 265
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,706评论 3 3
  • #import"LoadData.h" staticLoadData*ld; @implementationLoa...
    a52186211f97阅读 182评论 0 0
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 这是一个夏日的下午,太阳已经偏西,N市一百零五街马路和人行道上的阳光已被一傍十几层高的楼房挡住,只有楼层较...
    蝉鸣散人阅读 631评论 0 0