iOS 第三方框架-MJRefresh
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的更多相关文章
- iOS 第三方框架-SDWebImage
iOS中著名的牛逼的网络图片处理框架.包含的功能:图片下载.图片缓存.下载进度监听.gif处理等等.用法极其简单,功能十分强大,大大提高了网络图片的处理效率.国内超过90%的iOS项目都有它的影子. ...
- iOS:第三方框架MJPhotoBrowser图片浏览器的使用
介绍:MJPhotoBrowser这个第三方库是MJ老师封装的一套用来浏览图片的浏览器,可是是本地图片.网络图片.gif图片等,其也依赖了SDWebImage.SVProgressHUD.YLGIFI ...
- iOS 第三方框架-Masonry
介绍地址:http://www.cocoachina.com/ios/20141219/10702.html 官网:https://github.com/SnapKit/Masonry 记住:一定要先 ...
- 下拉刷新和上拉加载更多(第三方框架MJRefresh)
#import "RootViewController.h" #import "MJRefresh.h" @interface RootViewControll ...
- iOS - 第三方框架 - AFN
#5.AFNetworking 2.6使用方法 >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation >3.0版本 支持 iOS7以上 NSURLCo ...
- iOS 第三方框架-MJExtension
1.数组转换成模型数组 // 将 "微博字典"数组 转为 "微博模型"数组 NSArray *newStatuses = [HWStatus objectArr ...
- iOS 第三方框架-MBProgressHUD
MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分 ...
- iOS “智慧气象”APP中用到的第三方框架汇总
“智慧气象”是我最近在公司接手的项目,已经完成最新版本的更新并上架,在此分享下其中用到的第三方框架的使用. 应用地址:APP商店搜索“智慧气象” MJRefresh(下拉刷新)业界知名下拉刷新框架就不 ...
- iOS第三方Api及常用框架总结
iOS常用框架汇总: SVProgressHUD:产生覆盖层,禁止某种操作 SDWebImage: 专业下载图片框架 AFN:网络数据请求框架 MJExtension,模型对象之间互转 第三方分享第三 ...
随机推荐
- iPhone X的缺口和CSS
苹果公司(Apple)的发布会也开完了,新产品也将登陆了.估计很多开发人员看到iPhone X的设备是要崩溃了,特别对于前端开发人员更是如此. iPhone X的屏幕覆盖了整个手机的屏幕,为相机和其他 ...
- Android M App休眠 (adb shell dumpsys usagestats)
App休眠 在 Marshmallow 系统,Google 宣布了一个新的功能叫 App 休眠.App 休眠会阻止那些不 常用的 App(几天没有用过的 App)连接网络或者是运行任何程序直至设备充电 ...
- OpenStack cloud 第一天
这是刚接触openstack时候,看到的第一篇文章,感触很深,自己很喜欢的一个词Horizon就是出自本文 ============================================ ...
- 【CF840D】Destiny 分治(线段树)
[CF840D]Destiny 题意:给你一个长度为n的序列,q次询问,每次指定l r k,求[l,r]中出现次数$>\frac {r-l+1} k$的所有数中最小的那个数. $n,q\le 3 ...
- JPEG图片扩展信息读取与修改
extends:http://www.2cto.com/kf/201405/303813.html 读写均是键值对的方式,需要注意的是值的类型需要严格按照api定义格式. 支持读写节点为: 1.TAG ...
- Android开发中Chronometer的用法
Chronometer集成自TextView,里面有个Handler负责定时更新ui. 其计时原理很简单:通过setBase(long t)方法设置好baseTime之后,当start()时,每隔一秒 ...
- 慕课学习--DNS的作用
因为相对于32位的IP地址,人对域名更加敏感,也更容易记忆.所以一般都是把IP地址转化为域名进行网页的访问. DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映 ...
- PAT天梯赛L2-004 这是二叉搜索树吗【递归】
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- iOS + Node + MySQL
最近有空,又温习了一下Node ,配合Express 4.x可以很快的搭建一个简单的后台. Node比较适合频繁I/O,大量异步.至于更加复杂的后台逻辑还是用Java,个中滋味自己体验. Expres ...
- FW 每秒百万级别的 HTTP 请求 sung: 重型的(heavy-duty)、分布式的、多协议测试工具
本文是构建能够每秒处理 3 百万请求的高性能 Web 集群系列文章的第一篇.它记录了我使用负载生成器工具的一些经历,希望它能帮助每一个像我一样不得不使用这些工具的人节省时间. 负载生成器是一些生成用于 ...