使用开源库 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 = 0; i < 10; 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:0];

        //在tableView中插入一行(Row开头)
        [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0
                                                                inSection:0]]
                          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 - 1
                                                                inSection:0]]
                          withRowAnimation:UITableViewRowAnimationBottom];

        //结束更新
        [_tableView endUpdates];

        //停止菊花
        [_tableView.infiniteScrollingView stopAnimating];
    });
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (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

时间: 2024-08-21 19:57:09

使用开源库 SVPullToRefresh 实现上拉加载下拉刷新的相关文章

Android ListView实现上拉加载下拉刷新和滑动删除功能_Android

最近项目需要用到可以滑动删除并且带有上拉加载下拉刷新的Listview,查阅了一些资料,大多都是在SwipeMenuListView的基础上去添加头部和底部View,来扩展上拉加载和下拉刷新的功能,不过需要手动的去绘制UI及处理一些动画效果.用起来也不是特别方便.刚好项目中用到PulltorefreshLibrary库,就尝试着扩展了一个PullToRefreshSwipeMenuListView类来实现需求.先看一下效果: 实现步骤 一.组合Pulltorefresh与SwipeMenuLis

Android ListView实现上拉加载下拉刷新和滑动删除功能

最近项目需要用到可以滑动删除并且带有上拉加载下拉刷新的Listview,查阅了一些资料,大多都是在SwipeMenuListView的基础上去添加头部和底部View,来扩展上拉加载和下拉刷新的功能,不过需要手动的去绘制UI及处理一些动画效果.用起来也不是特别方便.刚好项目中用到PulltorefreshLibrary库,就尝试着扩展了一个PullToRefreshSwipeMenuListView类来实现需求.先看一下效果: 实现步骤 一.组合Pulltorefresh与SwipeMenuLis

上拉加载下拉刷新控件WaterRefreshLoadMoreView

上拉加载下拉刷新控件WaterRefreshLoadMoreView 效果: 源码: // // SRSlimeView // @author SR // Modified by JunHan on 13-9-18. // #import <UIKit/UIKit.h> #define kStartTo 0.7f #define kEndTo 0.15f #define kAnimationInterval (1.0f / 50.0f) NS_INLINE CGFloat distansBet

上拉记载-上拉加载下拉刷新refresher v2.0

问题描述 上拉加载下拉刷新refresher v2.0 5C 现在支持一个页面多个调用,这句话是什么意思啊,假如我一个页面里有一个大的div,里面有四个小的div,怎么实现让这四个小的div都可以下拉刷新,上拉加载呢× 解决方案 http://www.jq22.com/jquery-info4469 解决方案二: 上拉加载 下拉刷新iOS中tableView的下拉刷新与上拉加载(一)PullToRefreshListView下拉刷新与上拉加载

Android自定义listview布局实现上拉加载下拉刷新功能_Android

listview实现上拉加载以及下拉刷新的方式有很多.下面是我写的一种自定义的布局,复用性也比较的强.首先就是继承的listview的自定义view.      AutoListView.Java: package com.example.mic.testdemo.view; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.os

Android自定义listview布局实现上拉加载下拉刷新功能

listview实现上拉加载以及下拉刷新的方式有很多.下面是我写的一种自定义的布局,复用性也比较的强.首先就是继承的listview的自定义view. AutoListView.Java: package com.example.mic.testdemo.view; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.os.Bund

求高手指教,关于ajax动态加载下拉框的

问题描述 最近在弄毕业设计,是使用SSH框架开发的web项目.有一张课题表project,有一个projectlevel字段,字段为1,2,3,4时分别对应项目,课题,子课题,专题.并且通过fatherid字段相互关联.现在我要使用ajax实现一个这样的功能:有一个选择层级下拉框:里面有4个option:项目,课题,子课题,专题.当选择项目后,便出现一个下拉框:所属项目,并将projectlevel为1的projectname全部加载到所属项目下拉框中.当选择课题后,便出现2个下拉框,分别是所属

jqgrid 编辑时加载下拉框问题。

问题描述 数据是AJAX同步获取的,加载到下拉框里面会多出一个undifined?为啥?其它数据正常.,就是会多出一个undifined ,求大神解答.在下不胜感激啊..!部分代码见图片. 问题补充:conanca 写道 解决方案 第一张图片的字符串末尾多了个分号

寺库:线上商城加线下体验店 二手奢侈品的大生意

2008年,寺库创始人李日学准备做二手奢侈品业务,但他周围几乎没有人看好这个生意.二手奢侈品通常通过典当行或者那些零散的二手奢侈品寄卖店进行交易,但这从来不是一个可以做大的生意. 典当行的主营业务是放贷,是融资服务而不是商品交易,它更像是一家金融机构.而二手奢侈品店虽然直接面向消费市场,但由于在奢侈品鉴定和售后保养方面的不足或缺失,加上内地消费市场相对于香港和日本本身较为分散,所以很难做大. 寺库通过建立鉴定评估技术中心和养护中心,很好地解决了前期鉴定和后期养护的问题.同时通过线上商城+线下体验