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 实现上拉加载下拉刷新的更多相关文章

  1. APICloud上啦加载下拉刷新模块

    apicloud有自带的上啦加载下拉刷新,当让也可以用第三方或者在模块库里面找一个使用 一.下拉刷新,一下代码写在 apiready = function (){} 里面 apiready = fun ...

  2. Vue mint ui用在消息页面上拉加载下拉刷新loadmore 标记

    之前总结过一个页面存在多个下拉加载的处理方式,今天再来说一下在消息页面的上拉加载和下拉刷新,基本上每个app都会有消息页面,会遇到这个需求 需求:每次加载十条数据,上拉加载下拉刷新,并且没有点击查看过 ...

  3. 上拉加载下拉刷新控件WaterRefreshLoadMoreView

    上拉加载下拉刷新控件WaterRefreshLoadMoreView 效果: 源码: // // SRSlimeView // @author SR // Modified by JunHan on ...

  4. RecyclerView 上拉加载下拉刷新

    RecyclerView 上拉加载下拉刷新 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/teach_s ...

  5. 微信小程序上拉加载下拉刷新

    微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载 ...

  6. mui scroll和上拉加载/下拉刷新

    mui中 scroll和上拉加载/下拉刷新同时存在会出现两个滚动条 把/*   */ /* //mui页面鼠标拖动代码: mui('.mui-scroll-wrapper').scroll({ dec ...

  7. 基于better-scroll封装一个上拉加载下拉刷新组件

    1.起因 上拉加载和下拉刷新在移动端项目中是很常见的需求,遂自己便基于better-scroll封装了一个下拉刷新上拉加载组件. 2.过程 better-scroll是目前比较好用的开源滚动库,提供很 ...

  8. Flutter上拉加载下拉刷新---flutter_easyrefresh

    前言 Flutter默认不支持上拉加载,下拉刷新也仅仅支持Material的一种样式.Android开发使用过SmartRefreshLayout的小伙伴都知道这是一个强大的刷新UI库,集成了很多出色 ...

  9. SwipeRefreshLayout实现上拉加载下拉刷新

    package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...

  10. zepto.js + iscroll.js上拉加载 下拉加载的 移动端 新闻列表页面

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

随机推荐

  1. SQL Server 4

    一.视图 1.创建视图 1)选中数据库中的表中的视图处,右键选择新建视图,即: 2)在弹出“添加表”对话框中,单击“表”标签,选择要添加的表,点击添加,即: 3)选中要建立联系的列名的复选框,然后拖动 ...

  2. synchronized和lock

    Synchronized  同步代码块 使用 monitorenter 和 moniterexit 指令实现, monitorenter指令插入到同步代码块的开始位置, moniterexit 指令插 ...

  3. 主席树初步学习笔记(可持久化数组?静态区间第k大?)

    我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...

  4. 【Java】 大话数据结构(14) 排序算法(1) (冒泡排序及其优化)

    本文根据<大话数据结构>一书,实现了Java版的冒泡排序. 更多:数据结构与算法合集 基本概念 基本思想:将相邻的元素两两比较,根据大小关系交换位置,直到完成排序. 对n个数组成的无序数列 ...

  5. 000 在什么位置写js代码

    一:介绍 1.DOM 2.节点分类 节点及其类型: 1). 元素节点 2). 属性节点: 元素的属性, 可以直接通过属性的方式来操作. 3). 文本节点: 是元素节点的子节点, 其内容为文本. 二:J ...

  6. H5的简介

    1.H5的诞生 2.介绍 HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标准. HTML5 是 W3C 与 WHATWG 合作的结果. WHATWG 致力于 web 表单和应用 ...

  7. 阿里云服务器(windows)配置项目域名

    一.数据库是放在阿里云上!二.项目部署到服务器上,tomcat会一直运行(除非你手动停止)三.设置域名及tomcat配置(1)在阿里云控制台上的域名解析设置中添加记录,将域名映射到你的阿里云esc公网 ...

  8. luogu P1401 城市

    题目链接 luogu P1401 城市 题解 二分最小边权,dinic检验 代码 // luogu-judger-enable-o2 /* 二分最小边权,dinic检验 */ #include< ...

  9. BZOJ.1021.[SHOI2008]循环的债务(DP)

    题目链接 不同面额的钞票是可以分开考虑的. ↑其实并不很明白具体(证明?),反正是可以像背包一样去做. f[x][i][j]表示用前x种面额钞票满足 A有i元 B有j元 (C有sum-i-j)所需交换 ...

  10. BZOJ.1143.[CTSC2008]祭祀(Dilworth定理 最大流ISAP)

    题目链接 题目是求最长反链,反链指点集内任意两点不能互相到达. 根据Dilworth定理,在DAG中,\[最长反链 = 最小路径覆盖 = V - 最大匹配数\] 用Floyd求一遍传递闭包后,在所有可 ...