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. Nginx基本配置文件

    Nginx基本配置文件 1. 基本配置文件 /etc/nginx/nginx.conf # nginx运行的用户 user nginx; # nginx进程数,建议设置为等于CPU总核心数. work ...

  2. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

  3. 跟据经纬度实现附近搜索Java实现

    现在很多手机软件都用附近搜索功能,但具体是怎么实现的呢>在网上查了很多资料,mysql空间数据库.矩形算法.geohash我都用过了,当数据上了百万之后mysql空间数据库方法是最强最精确的(查 ...

  4. C++Primer #7 类

    类的定义 简单的来说类就是数据和它的操作的一种封装,内部提供接口函数 类的成员函数的声明必须在类的内部,而它的定义则既可以放在类的内部也可以放在类的外部.(一般在类内进行声明,类外实现函数定义) 定义 ...

  5. linux内核内存分配(三、虚拟内存管理)

    在分析虚拟内存管理前要先看下linux内核内存的具体分配我開始就是困在这个地方.对内核内存的分类不是非常清晰.我摘录当中的一段: 内核内存地址 ============================ ...

  6. 一步一步学习IdentityServer3 (3)

    在上一篇中配置一个基础的idrserver服务端 这篇文章将对服务端做一些变化,这里我先贴一下上一章中的代码 证书: static class Certificate { public static ...

  7. 电脑同时安装Python2和Python3以及virtualenvwrapper(转)

    电脑同时安装Python2和Python3以及virtualenvwrapper  https://www.jianshu.com/p/d22f19496e03   windows: 1 下载地址:P ...

  8. 2017, X Samara Regional Intercollegiate Programming Contest 题解

    [题目链接] A - Streets of Working Lanterns - 2 首先将每一个括号匹配串进行一次缩减,即串内能匹配掉的就匹配掉,每个串会变成连续的$y$个右括号+连续$z$个左括号 ...

  9. HNOI2018酱油记

    按照惯例,每次比赛完以后都要写酱油记. Day0: 明天就要省选了,今天同学们都回去了(因为后天要去春游),整个年级只剩下竞赛生.本来打算晚上好好复习一下,结果......颓了一晚上......(好吧 ...

  10. 踩过无数坑实现的哈夫曼压缩(JAVA)

    最近可能又是闲着没事干了,就想做点东西,想着还没用JAVA弄过数据结构,之前搞过算法,就试着写写哈夫曼压缩了. 本以为半天就能写出来,结果,踩了无数坑,花了整整两天时间!!orz...不过这次踩坑,算 ...