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. Mybatis 接口传入多个参数 xml怎么接收

    mybatis 在接口上传入多个参数 1.如果传入的参数类型一样. Map<String, String> queryDkpayBindBankCidByOriBindAndBankCid ...

  2. CF601A 【The Two Routes】

    看数据范围,然后果断邻接矩阵$Floyd$啊 对于公路和铁路,各建一个图,分别跑最短路,然后取最大值即可 #include<iostream> #include<cstdio> ...

  3. bzoj 1143

    求最长反链裸题 补充一点知识.. 链                  :    D 中的一个子集 C   满足 C 是全序集  及C中所有元素都可以比较大小 反链              :   ...

  4. 010.Zabbix的zatree插件安装

    一 zatree简介 zatree 是来自国内58公司开发的监控软件zabbix的一个插件,主要功能是提供host group的树形展示和在item里指定关键字查询及数据排序. 二 安装前准备 2.1 ...

  5. 006.Zabbix添加监控主机

    一 配置步骤和流程 Zabbix完整的监控配置流程可以简单的描述为: Host groups(主机组)---->Hosts(主机)---->Applications(监控项组)----&g ...

  6. JAVA 多线程制作大球吃小球 一、实现球的自动生成及运动 生产消费模型

    前几天用多线程实现了创建小球并移动,想到大鱼吃小鱼,便突发奇想要写一个大球吃小球.首先第一步自然是先把界面弄好啦 public class BallUI extends JPanel { privat ...

  7. 重读redux源码(一)

    前言 对于react技术栈的前端同学来说,redux应该是相对熟悉的.其代码之精简和设计之巧妙,一直为大家所推崇.此外redux的注释简直完美,阅读起来比较省事.原本也是强行读了通源码,现在也忘得差不 ...

  8. Android `AsyncTask`简要分析

    AsyncTask简要分析 经典异步任务:AsyncTask,使用场景有:批量下载,批量拷贝等.官方文档就直接给出了一个批量下载的示例. private class DownloadFilesTask ...

  9. Ubuntu下登陆远程postgresql数据库

    登陆公司远程postgresql:psql -h <host or remote id> -p <port> dbdame 如: psql -h  aliyunsql_addr ...

  10. C# Socket异步实现消息发送--附带源码

    前言 看了一百遍,不如动手写一遍. Socket这块使用不是特别熟悉,之前实现是公司有对应源码改改能用. 但是不理解实现的过程和步骤,然后最近有时间自己写个demo实现看看,熟悉熟悉Socket. 网 ...