使用开源库 SVPullToRefresh 实现上拉加载下拉刷新

SVPullToRefresh开源库地址
https://github.com/samvermette/SVPullToRefresh
将整个文件夹SVPullToRefresh拖入工程中并引入头文件即可
注意编译时有一个方法快被弃用了
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
工程源码
RootViewController.h
// Copyright (c) 2014年 YouXian. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.m
// Copyright (c) 2014年 YouXian. All rights reserved.
// #import "RootViewController.h"
#import "SVPullToRefresh.h" @interface RootViewController () <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; //初始化 tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; //初始化数据源
_dataSource = [[NSMutableArray alloc] init];
for (int i = ; i < ; i++)
{
[_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]];
} //注册下拉刷新功能
__weak RootViewController *weakSelf = self;
[_tableView addPullToRefreshWithActionHandler:^{
[weakSelf insertRowAtTop];
}]; //注册上拉刷新功能
[_tableView addInfiniteScrollingWithActionHandler:^{
[weakSelf insertRowAtBottom];
}];
} #pragma mark -
#pragma mark PullToRefreshInsertRow - (void)insertRowAtTop
{
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//开始更新
[_tableView beginUpdates]; //插入数据到数据源(数组的开头)
[_dataSource insertObject:[NSString stringWithFormat:@"%@", [NSDate date].description]
atIndex:]; //在tableView中插入一行(Row开头)
[_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:
inSection:]]
withRowAnimation:UITableViewRowAnimationBottom]; //结束更新
[_tableView endUpdates]; //停止菊花
[_tableView.pullToRefreshView stopAnimating];
});
} - (void)insertRowAtBottom
{
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//开始更新
[_tableView beginUpdates]; //插入数据到数据源(数组的结尾)
[_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]]; //在tableView中插入一行(Row结尾)
[_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_dataSource.count -
inSection:]]
withRowAnimation:UITableViewRowAnimationBottom]; //结束更新
[_tableView endUpdates]; //停止菊花
[_tableView.infiniteScrollingView stopAnimating];
});
} #pragma mark -
#pragma mark UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataSource.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
} cell.textLabel.text = _dataSource[indexPath.row]; return cell;
} @end
心得:
使用简单,逻辑清晰,开源库使用block实现, RootViewController.m 35行代码处要将RootViewController自身传入block中,需要使用弱应用指针,注意.
工程源码地址:
http://pan.baidu.com/s/1dD24E1V
使用开源库 SVPullToRefresh 实现上拉加载下拉刷新的更多相关文章
- APICloud上啦加载下拉刷新模块
apicloud有自带的上啦加载下拉刷新,当让也可以用第三方或者在模块库里面找一个使用 一.下拉刷新,一下代码写在 apiready = function (){} 里面 apiready = fun ...
- Vue mint ui用在消息页面上拉加载下拉刷新loadmore 标记
之前总结过一个页面存在多个下拉加载的处理方式,今天再来说一下在消息页面的上拉加载和下拉刷新,基本上每个app都会有消息页面,会遇到这个需求 需求:每次加载十条数据,上拉加载下拉刷新,并且没有点击查看过 ...
- 上拉加载下拉刷新控件WaterRefreshLoadMoreView
上拉加载下拉刷新控件WaterRefreshLoadMoreView 效果: 源码: // // SRSlimeView // @author SR // Modified by JunHan on ...
- RecyclerView 上拉加载下拉刷新
RecyclerView 上拉加载下拉刷新 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/teach_s ...
- 微信小程序上拉加载下拉刷新
微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载 ...
- mui scroll和上拉加载/下拉刷新
mui中 scroll和上拉加载/下拉刷新同时存在会出现两个滚动条 把/* */ /* //mui页面鼠标拖动代码: mui('.mui-scroll-wrapper').scroll({ dec ...
- 基于better-scroll封装一个上拉加载下拉刷新组件
1.起因 上拉加载和下拉刷新在移动端项目中是很常见的需求,遂自己便基于better-scroll封装了一个下拉刷新上拉加载组件. 2.过程 better-scroll是目前比较好用的开源滚动库,提供很 ...
- Flutter上拉加载下拉刷新---flutter_easyrefresh
前言 Flutter默认不支持上拉加载,下拉刷新也仅仅支持Material的一种样式.Android开发使用过SmartRefreshLayout的小伙伴都知道这是一个强大的刷新UI库,集成了很多出色 ...
- SwipeRefreshLayout实现上拉加载下拉刷新
package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...
- zepto.js + iscroll.js上拉加载 下拉加载的 移动端 新闻列表页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
随机推荐
- InnoDB master thread工作原理
我们简单交流下InnoDB master thread学习,有兴趣的朋友可以参考<<MySQL技术内蒙--InnoDB存储引擎第二版>> void master_thread( ...
- android studio 3.0之后版本自定义文件名生成apk文件
修改app模块的build.gradle 在android闭包中添加以下代码 //指定打包后应用名称 android.applicationVariants.all { variant -> v ...
- 【CodeChef】QTREE- Queries on tree again!
题解 给你一棵基环树,环长为奇数(两点间最短路径只有一条) 维护两点间路径最大子段和,支持把一条路径上的值取反 显然只要断开一条边维护树上的值,然后对于那条边分类讨论就好了 维护树上的值可以通过树链剖 ...
- ubuntu ifconfig只有lo没有ens33的问题
如果ifconfig只显示了lo, ifconfig -a 却正常显示ens33.那么可以按照如下的操作: service network-manager stop rm /var/lib/Netwo ...
- java 的反射机制
一:介绍 1.大纲 #1 允许程序在执行期间,调用反射API取得任何类的内部信息,并且可以直接操作任何对象的内部属性和方法. #2 学习反射,需要掌握的知识点: *实例化class类 *获取类的完整结 ...
- ref:linux查看用户登录时间以及命令历史
ref:https://blog.csdn.net/csdn924618338/article/details/73555725/ 1.查看当前登录用户信息 who命令: who缺省输出包括用户名.终 ...
- cv2 与 matplotlib 的 Bug 记录
cv2 的 imread 无法读取中文路径 解决方案: img = cv2.imdecode(np.fromfile(image_path,dtype=np.uint8),cv2.IMREAD_COL ...
- linux学习笔记-6.权限
1.创建a.txt和b.txt文件,将他们设为其拥有者和所在组可写入,但其他以外的人则不可写入 chmod ug+w,o-w a.txt b.txt 2.创建c.txt文件所有人都可以写和执行 chm ...
- jstat命令总结
jvm统计信息监控工具 一. jstat是什么 jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool ...
- Redis客户端连接以及持久化数据(三)
0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...