美文网首页
2018-05-31

2018-05-31

作者: 刘半仙333 | 来源:发表于2018-05-31 14:49 被阅读0次

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 ;

}

相关文章

网友评论

      本文标题:2018-05-31

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