美文网首页
SDWebImage+MJRefresh+MBBrogressH

SDWebImage+MJRefresh+MBBrogressH

作者: wang_47da | 来源:发表于2018-05-31 08:35 被阅读0次

    第一步:依次导入MBProgressHUD、MJRefresh、SDWebImage、YYModel库

    第二步:AppDelegate.m

    导入头文件 ViewController.m

    ViewController * vc = [[ViewController alloc]init];

    UINavigationController * vcNav = [[UINavigationController alloc]initWithRootViewController:vc];

    第三步:创建Mymessage继承NSObject

    Mymessage.h中

    // 第四层

    @interface Mylist : NSObject

    @property (nonatomic,strong) NSString * title ;

    @property (nonatomic,strong) NSString * time ;

    @property (nonatomic,strong) NSString * pic ;

    @end

    // 第三层

    @interfacemyresult :NSObject

    @property (nonatomic,strong) NSString * channel ;

    @property (nonatomic,strong) NSArray * list ;

    @end

    // 第二层

    @interfaceMYresult :NSObject

    @property (nonatomic,strong) myresult * wresult ;

    @end

    // 第一层

    @interfaceMymessage :NSObject

    @property (nonatomic,strong) MYresult * qresult ;

    @end

    Mymessage.m中

    @implementation Mylist

    @end

    // 第三层

    @implementation myresult

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

    + (NSDictionary*)modelContainerPropertyGenericClass {

        return@{@"list": [Mylistclass]

                 };

    }

    @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.h中更改UITableViewController

    第五步:在ViewController.m中导入头文件

    #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"

    签协议  MJRefreshBaseViewDelegate

    {

        //初始化类

        Mymessage*  message;

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

       __blockNSMutableArray* arr1 ,*arr2 ,*arr3;

        // 上下拉刷新

         MJRefreshFooterView *foot;

        MJRefreshHeaderView * header ;

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

        inti ;

        // cell 中添加的图片框

        UIImageView* img ;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        i=5;

        // 刷新数据

        [selfshuaxin];

    }

    // 刷新显示数据

    -(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([refreshViewisKindOfClass:[MJRefreshHeaderViewclass]]) {

            //2秒以后调用

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

                // 10    1

                // 请求数据

                [selfrequest];

                [self.tableViewreloadData];

                // 结束

                [headerendRefreshing];

            });

        }else{

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

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

                i=arr1.count;

                [self.tableViewreloadData];

                [footendRefreshing];

            });

        }

    }

    -(void)request

    {

        // 转换编码格式

        NSString * str = [URL stringByAddingPercentEscapesUsingEncoding:

                          NSUTF8StringEncoding];

        // 请求数据

        NSURLSession * session = [NSURLSession sharedSession];

        NSURLSessionTask* task  = [sessiondataTaskWithURL:[NSURLURLWithString:str]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {

            arr1= [[NSMutableArrayalloc]init];

            arr2= [[NSMutableArrayalloc]init];

            arr3= [[NSMutableArrayalloc]init];

            // yymodel  解析

            message= [Mymessageyy_modelWithJSON:data];

            // 遍历数据

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

                // 加入到数组中

                [arr1addObject:list.title];

                [arr2addObject:list.time];

                [arr3addObject:list.pic];

            }

            // 回到主线程刷新表格

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.tableViewreloadData];

            });

        }];

        // 开始请求

        [taskresume];

    }

    - (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 = [NSStringstringWithFormat:@"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.contentViewaddSubview: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.contentViewaddSubview: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 这个参数是用来设置 还没有请求到图片之前展示的图片信息

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

        [imgsetImageWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"%@",arr3[indexPath.row]]]placeholderImage:nilcompleted:^(UIImage*image,NSError*error,SDImageCacheTypecacheType) {

                img.image= image ;

        }];

        // 调用加载的进度方法

        [selfHUD] ;

        [cell.contentViewaddSubview:img];

        returncell ;

    }

    // 正在加载的进度

    -(void)HUD

    {

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

        //显示hud的模式

        hud.mode = MBProgressHUDAnimationFade;

        //背景颜色

        hud.color= [UIColorgrayColor];

        //主标题

        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当中

        [imgaddSubview:hud];

        [hudshowAnimated:YES whileExecutingBlock:^{

            //hud执行期间

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

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

            //hud执行完毕

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

        }];

    }

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

    {

        return150;

    }

    相关文章

      网友评论

          本文标题:SDWebImage+MJRefresh+MBBrogressH

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