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. 原生js--cookie操作的封装

    封装cookie的操作:查询cookie个数.查询所有cookie的键.获取cookie.设置cookie.删除cookie.清除全部cookie /** * cookieStorage */func ...

  2. DOS 如何取当前时间做为文件名?

    如果要取得以日期为文件名的文件,假设在命令行下键入date返回形式为:当前日期: 2005-06-02 星期四echo > %date:~0,4%%date:~5,2%%date:~8,2%~表 ...

  3. laravel读取memcached缓存并做条件查询

    public function onlineplayersource() { $res = $_POST['aoData']; $sEcho = 0; $iDisplayStart = 0; // 起 ...

  4. php 加密压缩

    php 把文件打成压缩包 ,可以去搜下 pclzip 搜很好多地方没有找到对压缩包进行加密操作的. 如果服务器是linux 那么见代码: $filename="test.csv"; ...

  5. SQLite 3的中文读写

    调用sqlite3_open函数默认创建的数据库encoding=UTF-8,执行sqlite3_exec时需要将对应的字符串转换为UTF-8格式多字节字符串.比如: sqlite3* db; aut ...

  6. MacOS 安装 nginx

    brew install nginx 开机启动 $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDae ...

  7. 23种设计模式之建造者模式(Builder)

    建造者模式强调将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一步一步地创建一个复杂的对象,它允许用户只通过制定复杂对象的类型和内容就可以构建它们,用户不需要知道 ...

  8. NET的堆和栈04,对托管和非托管资源的垃圾回收以及内存分配

    在" .NET的堆和栈01,基本概念.值类型内存分配"中,了解了"堆"和"栈"的基本概念,以及值类型的内存分配.我们知道:当执行一个方法的时 ...

  9. springMVC 报错:Unknown return value type: java.lang.Integer

    controller层返回值类型为Integer,运行报错: Unknown return value type: java.lang.Integer 解决办法:在此方法上写上注解 @Response ...

  10. 【转】UTF16和UTF8什么区别?

    这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级.整理这篇文章的动机是两个问题: 问题一: 使用Windows记事本的“另存为 ...