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

#import "ViewController.h"

#import "MJRefresh/MJRefresh.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{

    //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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • 如毁昊天木欲焦 稻粱王事倍忧劳 筐承老耄弃林壑 未若存薪羁二毛
    Psych阅读 219评论 3 1
  • 【0410能量按钮】 推荐人:五连老铁温暖阳光 情绪是生命的资源,天下没有什么情绪是魔鬼,所有的情绪本质上都是天使...
    尧建红阅读 298评论 0 3
  • 女人就这样,不能有过节,一旦消除了过节,再好起来,就没边了。恨不得把自己的脑袋割下来,再装到对方的脖子上去。 盲人...
    七泠阅读 240评论 0 0
  • 清风阵阵夜微凉,晓月如霜撒东窗,两三壶,桃花酿,伊人醉卧枝头上。 十里桃林落芬芳,碧水春江惹残香,浅红妆,白衣裳,...
    山有木兮木有枝_08ea阅读 106评论 0 0
  • 深夜你来,不要你带酒,只需花你一盏茶的时间,听我讲一个故事就好。 我是大家口中比较流行的“佛系”少女。拿着保温杯泡...
    居宝宝宝宝阅读 406评论 0 0