MJMJRefresh、SDWebImage

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

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,101评论 3 38
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,735评论 2 7
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,690评论 8 265
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,739评论 3 3