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,模型对象之间互转 第三方分享第三 ...
随机推荐
- eagle学习汇总
一.原理图编辑器 1. 编辑->全局属性->可以设置全局变量,选择“文本框”,以‘>’开头代表引用全局属性的值. 2. 绘制->Frame->可绘制原理图边框,一般选择“ ...
- jQuery缓存机制(四)
Data封装的方法的后面四个方法 和 dataAttr方法阅读. Data.prototype = { key: function( owner ) {}, set: function( owner, ...
- mac Intellij Idea Tmocat 启动报 Error running Tomcat: /conf/Catalina
原因:主要是tomcat下Catalina目录没有权限导致,将其设置读写权限即可 如果在刚刚启动tomcat时出现以下问题:Error running Tomcat 8.5.31: Error cop ...
- [原]linux下将网卡设置为混杂模式
设置为混杂模式ifconfig eth2 promisc取消设置ifconfig eth2 -promisc ------------------------------------------ 下面 ...
- zabbix配置server,proxy,agent架构
author: headsen chen date:2018-10-30 19:49:50 环境: centos 6.8_x86_64 zabbix-server: 192.168.1.130 z ...
- jquery的ajax()函数中文传值出现乱码完美解决方案
1. jquery的ajax()函数 $.ajax({ type: "POST", dataType: "text", url: ".. ...
- Nhibernate学习
Contains 缓存中是否存在SessionFactoryBizCom.GetInstance().GetCurrentSession().Contains(t1) Evict 临时状态(Trans ...
- html5播放器制作小结
链接:http://snowinmay.net/6rooms/html/music.php 9月份前的版本: 播放,暂停,点赞,播放状态显示. 9.2版本: 下载歌曲,静音,时间倒计时(点击暂停时倒计 ...
- [转][darkbaby]任天堂传——失落的泰坦王朝(上)
前言: 曾经一再的询问自我;是否真的完全了解任天堂这个游戏老铺的真实本质?或许从来就没有人能够了解,世间已经有太多的真相被埋没在谎言和臆测之中.作为 一个十多年游龄的老玩家,亲眼目睹了任天堂从如日 ...
- POJ-1088 滑雪 (记忆化搜索,dp)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 Accepted: 32289 Description Mich ...