美文网首页
MJMJRefresh、SDWebImage

MJMJRefresh、SDWebImage

作者: 呵邢 | 来源:发表于2018-06-29 14:04 被阅读0次

    1.布局

    Main.storyboard

    继承于UITableViewCell

    myTableViewCell.xib

    拖拽控件

    2.

    #import "ViewController.h"

    #import "myTableViewCell.h"

    #import "UIImageView+WebCache.h"

    #import "MJRefresh.h"

    #import "MJRefreshFooter.h"

    #import "MJRefreshAutoFooter.h"

    #import "MJRefreshHeader.h"

    #import "MBProgressHUD.h"

    @interface ViewController ()

    {

        UITableView *tbv;

        NSMutableDictionary *dic;

        NSArray *arr;

        int  i;

    }

    @ end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // 创建表格

        tbv = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

        // 设置行高

        tbv.rowHeight = 200;

        // 设置数据源和代理

        tbv.delegate=self;

        tbv.dataSource = self;

        // 添加到主界面

        [self.view  addSubview: tbv];

        NSURLSession *session = [NSURLSession sharedSession];

        NSString *filePath = @"https://way.jd.com/jisuapi/get?channel=头条&num=10&start=0&appkey=54e619938bc38b40151c7bc35a29067e";

        NSString *str1 = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

            // JSON解析

            dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            dispatch_async(dispatch_get_main_queue(), ^{

                // 刷新表格

                [tbv  reloadData];

            });

        }];

        // 开始请求

        [task  resume];

        // 使用MJRefresh,实现下拉重新加载数据的功能

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];

        //    [header setTitle:@"正在努力加载中! ! !" forState:MJRefreshStateRefreshing];

        tbv.mj_header= header;

        // 使用MJRefresh,实现上拉加载更多数据的功能

        MJRefreshBackFooter *footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];

        tbv.mj_footer= footer;

    }

    - (void) GetNetWorking{

        NSURLSession *session = [NSURLSession sharedSession];

        NSString *str3 = [NSString stringWithFormat:@"https://way.jd.com/jisuapi/get?channel=头条&num=10&start=%d&appkey=54e619938bc38b40151c7bc35a29067e",i];

        NSString *str1 = [str3 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

            // JSON解析

            dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            dispatch_async(dispatch_get_main_queue(), ^{

                // 刷新表格

                [tbvreloadData];

            });

        }];

        // 开始请求

        [taskresume];

    }

    // 实现下拉刷新的方法

    -  (void) header{

        MBProgressHUD *progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

        // 每次开始发送请求时,都使用MBBrogressHUD显示Loading窗口

        [progresssetMinShowTime:2];

        // 请求完毕后关闭MBBrogressHUD的加载效果

        [progresshide:YES];

        i = 0;

        [self   GetNetWorking];

        //

        [tbv.mj_header endRefreshing];

        //

    }

    // 实现上拉刷新的方法

    -  (void) footer{

        i = i + 1;

        [self   GetNetWorking];

        [tbv.mj_footer endRefreshing];

    }

    // 实现数据源方法

    // 几组

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

        return 1;

    }

    // 几行

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

        arr = dic[@"result"][@"result"][@"list"];

        returnarr.count;

    }

    // 设置cell内容

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

        staticNSString*str =@"cell";

        myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

        if(!cell) {

            cell = [[[NSBundle mainBundle]loadNibNamed:@"myTableViewCell" owner:self options:nil]firstObject];

        }

        //    将解析到的标题和时间显示到UITableView上

        NSDictionary*dic1 =arr[indexPath.row];

        cell.title.text= dic1[@"title"];

        // 自动换行

        cell.title.numberOfLines=0;

        // 添加时间

        cell.time.text= dic1[@"time"];

        // 时间居中

        cell.time.textAlignment = NSTextAlignmentCenter;

        // 请求数据后,提示用户数据请求成功或失败

        cell.succ.text=dic[@"msg"];

        // 使用SDWebImage,将图片显示到UITableView上

        NSString*str2 = dic1[@"pic"];

        NSURL*url = [NSURLURLWithString:str2];

        [cell.imgV sd_setImageWithURL:url];

        returncell;

    }

    @ end

    相关文章

      网友评论

          本文标题:MJMJRefresh、SDWebImage

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