MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单。github地址: https://github.com/CoderMJLee/MJRefresh 。

下拉刷新

官方给过来的例子很简单,默认使用如下:

self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];

// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)]; // 马上进入刷新状态
[self.tableView.header beginRefreshing];

结束下拉刷新:

// 拿到当前的下拉刷新控件,结束刷新状态
[self.tableView.header endRefreshing];

上拉刷新

官方给过来的默认例子:

self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
}];

// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法)
self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

结束上拉刷新:

// 拿到当前的上拉刷新控件,结束刷新状态
[self.tableView.footer endRefreshing];

从上面,我们可以看到,一般情况下,进行页面的时候,我们会使用下拉刷新,并“马上进入刷新状态”,网络请求完数据后,结束“下拉刷新状态”。但上拉刷新,就不需要“马上进入刷新状态”了。

更多使用例子,请参数官方给过来的例子,使用起来还是挺方便的。

下面,给出某个项目的实际使用代码:

//
// NJBillTableViewController.m
// NJWisdomCard
//
// Created by admin on 15/8/21.
// Copyright (c) 2015年 Weconex. All rights reserved.
// #import "NJBillTableViewController.h"
#import "NJBillListTableviewCell.h"
#import "Common.h"
#import "NJHttpToolHandle.h"
#import "MBProgressHUD+NJ.h"
#import "NJAccountTool.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "NJAccountModel.h"
#import "NJBillModel.h" @interface NJBillTableViewController()
/**
* 账单模型
*/
@property (nonatomic, strong) NSMutableArray *billsFrames;
/**
* 页数
*/
@property (nonatomic,assign) int pageIndex; @end @implementation NJBillTableViewController - (NSMutableArray *)billsFrames
{
if (!_billsFrames) {
self.billsFrames = [NSMutableArray array];
}
return _billsFrames;
} - (void)viewDidLoad {
[super viewDidLoad]; //集成下拉刷新控件
[self setupDownRefresh]; //集成上拉刷新控件
[self setupUpRefresh];
} /**
* 集成上拉刷新控件
*/
- (void)setupUpRefresh
{
self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
} /**
* 集成下拉刷新控件
*/
- (void)setupDownRefresh
{
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewBills)]; // 马上进入刷新状态
[self.tableView.header beginRefreshing];
}
/**
* 加载下拉刷新数据
*/
- (void)loadNewBills
{
_pageIndex=;//默认加载第一页
[self.billsFrames removeAllObjects];//移除所有的数据 //1.从沙盒里拿用户模型
NJAccountModel *accountModel=[NJAccountTool accountModel]; // 2.拼接请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"userId"] =accountModel.loginId;//登录号
params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//页数
params[@"pagesize"] =[NSString stringWithFormat:@""]; //3.发送请求
[NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) {
if ([responseObject[@"resultCode"] isEqualToString:@""]) { //获取value数组
NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil]; // 将 "账单字典"数组 转为 "账单模型"数组
NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]]; // 将最新的账单数据,添加到总数组的最前面
NSRange range = NSMakeRange(, newBills.count);
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
[self.billsFrames insertObjects:newBills atIndexes:set];
//[self.billsFrames insertObjects:newBills atIndex:0];
//[self.billsFrames insertObjects:newBills atIndexes:0]; // 刷新表格
[self.tableView reloadData]; // 拿到当前的下拉刷新控件,结束刷新状态
[self.tableView.header endRefreshing];
}
else
{
[self.tableView.header endRefreshing];
} } failure:^(NSError *error) {
[self.tableView.header endRefreshing];
}];
} /**
* 加载上拉刷新数据
*/
-(void)loadMoreBills
{
//1.设置页数
_pageIndex++;//默认加载第一页 // 2.拼接请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"userId"] =[NJAccountTool accountModel].loginId;//登录号
params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//页数
params[@"pagesize"] =[NSString stringWithFormat:@""]; //3.发送请求
[NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) {
if ([responseObject[@"resultCode"] isEqualToString:@""]) { //获取value数组
NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil]; // 将 "账单字典"数组 转为 "账单模型"数组
NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]]; // 将更多的账单数据,添加到总数组的最后面
[self.billsFrames addObjectsFromArray:newBills]; // 刷新表格
[self.tableView reloadData]; // 拿到当前的下拉刷新控件,结束刷新状态
[self.tableView.footer endRefreshing];
}
else
{
[self.tableView.footer endRefreshing];
} } failure:^(NSError *error) {
[self.tableView.footer endRefreshing];
}];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NJBillListTableviewCell *cell = [NJBillListTableviewCell cellWithTableView:tableView];
cell.billModel=self.billsFrames[indexPath.item];
return cell;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.billsFrames.count;
} -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
} @end

碰到的坑

1、上拉刷新多次加载

是因为 [self.tableView reloadData] 要放在  endRefreshing 前面。

iOS 第三方框架-MJRefresh的更多相关文章

  1. iOS 第三方框架-SDWebImage

    iOS中著名的牛逼的网络图片处理框架.包含的功能:图片下载.图片缓存.下载进度监听.gif处理等等.用法极其简单,功能十分强大,大大提高了网络图片的处理效率.国内超过90%的iOS项目都有它的影子. ...

  2. iOS:第三方框架MJPhotoBrowser图片浏览器的使用

    介绍:MJPhotoBrowser这个第三方库是MJ老师封装的一套用来浏览图片的浏览器,可是是本地图片.网络图片.gif图片等,其也依赖了SDWebImage.SVProgressHUD.YLGIFI ...

  3. iOS 第三方框架-Masonry

    介绍地址:http://www.cocoachina.com/ios/20141219/10702.html 官网:https://github.com/SnapKit/Masonry 记住:一定要先 ...

  4. 下拉刷新和上拉加载更多(第三方框架MJRefresh)

    #import "RootViewController.h" #import "MJRefresh.h" @interface RootViewControll ...

  5. iOS - 第三方框架 - AFN

    #5.AFNetworking 2.6使用方法 >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation >3.0版本 支持 iOS7以上 NSURLCo ...

  6. iOS 第三方框架-MJExtension

    1.数组转换成模型数组 // 将 "微博字典"数组 转为 "微博模型"数组 NSArray *newStatuses = [HWStatus objectArr ...

  7. iOS 第三方框架-MBProgressHUD

    MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分 ...

  8. iOS “智慧气象”APP中用到的第三方框架汇总

    “智慧气象”是我最近在公司接手的项目,已经完成最新版本的更新并上架,在此分享下其中用到的第三方框架的使用. 应用地址:APP商店搜索“智慧气象” MJRefresh(下拉刷新)业界知名下拉刷新框架就不 ...

  9. iOS第三方Api及常用框架总结

    iOS常用框架汇总: SVProgressHUD:产生覆盖层,禁止某种操作 SDWebImage: 专业下载图片框架 AFN:网络数据请求框架 MJExtension,模型对象之间互转 第三方分享第三 ...

随机推荐

  1. Python守护进程和脚本单例运行

    Python 守护进程 守护进程简介 进程运行有时候需要脱离当前运行环境,尤其是Linux和Unix环境中需要脱离Terminal运行,这个时候就要用到守护进程.守护进程可以脱离当前环境要素来执行,这 ...

  2. 如何分离p12(或pfx)文件中的证书和私钥

    p12(或者pfx)文件里一般存放有CA的根证书,用户证书和用户的私钥 假设我们有一个test.p12文件 在安装了openssl的linux服务器上执行以下命令: 提取用户证书: openssl p ...

  3. 【CF662C】Binary Table 按位处理

    [CF662C]Binary Table 题意:给你一个$n\times m$的01网格,你可以进行任意次操作,每次操作是将一行或一列的数都取反,问你最多可以得到多少个1? $n\le 20,m\le ...

  4. [工具] CuteMarkEd

    CuteMarkEd 是一款开源免费的.支持代码高亮的.朴素的 Markdown 本地编辑器,支持 Windows.Linux. 就因为程序员喜欢用,然后就拼命的开发 Markdown 编辑器么?青小 ...

  5. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  6. 一个js文件如何加载另外一个js文件

    方法一,在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></scrip ...

  7. C++类继承示例

    C++的子类与孙子类都实现了虚函数时,孙子类的实现会覆盖掉子类的实现. 继承的最主要的应用就是把不同的类放到一个数组中,然后遍历调用同名函数. 实例如下: #include <iostream& ...

  8. java不足前面补0

    // 0 代表前面补充0 // 3代表长度为3 // d 代表参数为正数型 result=String.format("%0"+3+"d",result);

  9. vue之介绍

    vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...

  10. list,set中可以存放Object类型对象

    List<JSONObject> series = new ArrayList<JSONObject>();