上下拉刷新,需要到导入一个MJRefresh

#import "ViewController.h"

#import "MJRefresh/MJRefresh.h"

@interface ViewController (){

    //n表格数组

    NSMutableArray * tableArray;

    //页码

    int _curPage;

    //时间

    NSString * curTime;

    //下拉刷新

    MJRefreshHeaderView * mjHeadVC;

    //上啦刷新

    MJRefreshFooterView * mjFooterVC;

}

@property (nonatomic ,strong)UITableView *tbv;

@end

@implementation ViewController

-(UITableView *)tbv{

    if (!_tbv) {

        _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        _tbv.delegate = self;

        _tbv.dataSource = self;

    }

    return _tbv;

}

#pragma mark -------- UITableDelegate------

#pragma mark -------- UITableDasource-------

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

    return tableArray.count;

}

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

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"2"];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"2"];

    }

    //得到每一个cenll对应的字典

    NSDictionary * cellDIC =tableArray[indexPath.row];

    cell.textLabel.text=cellDIC[@"content"];

    cell.textLabel.numberOfLines = 0;



    return cell;

}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    //请求数据

    [self requestDataPost:_curPage time:curTime];


}

#pragma mark  ------------请求网络数据---------

//get方式请求数据

-(void)requestDataGet:(int)page time:(NSString*)time{


}

//post方法

-(void)requestDataPost:(int)page time:(NSString*)time{

    //显示状态栏中的z等待想时期

    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

    NSURL * url = [NSURL URLWithString:@"http://v.juhe.cn/joke/content/list.php"];

    NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0];

    [req setHTTPMethod:@"POST"];

    NSString * pargerStr = [NSString stringWithFormat:@"key=%@&page=%d&pagesize=10&sort=desc&time=%@",@"eaaf69cdca2f46e403a264f5ef7cb74b",_curPage,curTime];

    [req setHTTPBody:[pargerStr dataUsingEncoding:NSUTF8StringEncoding]];

    [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        //回到主线程

        dispatch_async(dispatch_get_main_queue(), ^{

            //停止状态栏中的等待指示控件

            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

            //y停止下拉刷新控件

            [self->mjHeadVC endRefreshing];

          //停止上啦刷新控件

            [self->mjFooterVC endRefreshing];

        });

        if (error !=nil) {

            NSLog(@"服务器错误");

            return ;

        }

        NSError * jsonerror = nil;

        id jsData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];

        if (jsonerror !=nil) {

            NSLog(@"网络错误");

            return;

        }

        if ([jsData[@"error_code"]integerValue] !=0) {

            NSLog(@"%@",jsData[@"reason"]);

            return;

        }

        NSArray * dataARR = [jsData[@"result"]objectForKey:@"data"];

        if (self->_curPage==1) {

              self->tableArray = [dataARR mutableCopy];

        }else{

            [self->tableArray addObjectsFromArray:dataARR];

        }


        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tbv reloadData];

        });

    }]resume];


}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.view addSubview:self.tbv];

    //给当前页码默认值

    _curPage = 1;

    //给时间字符串赋值

    NSDate * now = [NSDate date];

    NSTimeInterval interval =[now timeIntervalSince1970];

    curTime = [NSString stringWithFormat:@"%d",(int)interval];

    //将下拉刷新实现

    mjHeadVC = [[MJRefreshHeaderView alloc]initWithScrollView:self.tbv];

    //使用week解决循环强引用

    __weak typeof (self) weakSelf = self;

    //定义下拉回调的代码块

    mjHeadVC.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {

        //用一个强引用对weak修饰  防止内存释放

        __strong typeof (self) strongSelf = weakSelf;

        //重新初始化页码和当前时间

        self->_curPage=1;

        NSTimeInterval interval =[now timeIntervalSince1970];

        self->curTime = [NSString stringWithFormat:@"%d",(int)interval];

        //重新获取网络数据

        [strongSelf requestDataPost:strongSelf->_curPage time:strongSelf->curTime];

    };

    //上啦刷新l控件

    mjFooterVC = [[MJRefreshFooterView alloc]initWithScrollView:self.tbv];

    //设置回调代码块

    mjFooterVC.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {

        //用一个强引用对weak修饰  防止内存释放

        __strong typeof (self) strongSelf = weakSelf;

        //改变页码

        self->_curPage+=1;

        //重新获取网络数据

        [strongSelf requestDataPost:strongSelf->_curPage time:strongSelf->curTime];

    };

}

@end

你可能感兴趣的:(上下拉刷新,需要到导入一个MJRefresh)