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 Tkinter Text控件

    原文地址: http://blog.csdn.net/bemorequiet/article/details/54743889 这篇博客主要是简单的说一下Tkinter中的Text控件的相关知识. T ...

  2. Delete触发器

  3. 斐讯K2刷不死breed与第三方固件教程

    本文主要就是简单的斐讯 K2 刷机教程,方便大家了解一下 K2 怎样刷固件.斐讯 K2 是一款 1200M AC 双频无线路由器,支持 5G 和 2.4G WiFi 信号,虽然缺少 USB 且只有百兆 ...

  4. Storm启动流程分析

    1. 客户端运行storm nimbus时,会调用storm的python脚本,该脚本中为每个命令编写一个方法,每个方法都可以生成一条相应的java命令. 命令格式如下:java -server xx ...

  5. mongodb的远程访问

    1,centos6上安装mongodb:2,新建可以远程访问的用户,以便可以远程访问: [root@localhost ~]# cd /usr/local/mongodb/bin/ [root@loc ...

  6. C# 压缩文件 ICSharpCode.SharpZipLib.dll

    效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...

  7. CMOS构成的常见电路

    CMOS门电路 以MOS(Metal-Oxide Semiconductor)管作为开关元件的门电路称为MOS门电路.由于MOS型集成门电路具有制造工艺简单.集成度高.功耗小以及抗干扰能力强等优点,因 ...

  8. ELK到底是什么?那么多公司用!

    Sina.饿了么.携程.华为.美团.freewheel.畅捷通 .新浪微博.大讲台.魅族.IBM...... 这些公司都在使用ELK!ELK!ELK! ELK竟然重复了三遍,是个什么?   一.ELK ...

  9. node path的几个路径问题

    __dirname: 总是返回被执行的 js 所在文件夹的绝对路径 __filename: 总是返回被执行的 js 的绝对路径 process.cwd(): 总是返回运行 node 命令时所在的文件夹 ...

  10. ASP.NET IIS System.UnauthorizedAccessException: 对路径“C:\......xls”的访问被拒绝。

    问题: System.UnauthorizedAccessException: 对路径“C:\.....xls”的访问被拒绝. 背景: 项目中用到Excel导出功能,用的是Excel模板的方式来做,意 ...