使用开源库 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

目录
相关文章
|
9月前
uniapp上拉加载更多
uniapp上拉加载更多
131 0
|
9月前
|
Dart
Flutter EasyRefreshList使用方法 下拉加载 上拉刷新
Flutter EasyRefreshList使用方法 下拉加载 上拉刷新
Flutter控件之CircularProgressIndicator
Flutter控件之CircularProgressIndicator
|
Android开发 计算机视觉 Kotlin
Compose 实现下拉刷新和上拉加载
Compose 实现下拉刷新和上拉加载
1059 0
|
前端开发
Flutter 之列表下拉刷新和上拉加载
在实际的 App 中,下拉刷新和上滑加载更多是非常常见的交互形式。在 Flutter 中,有 flutter_easyrefresh开源插件用于实现下拉刷新和上滑加载更多。本篇介绍了有状态组件和 flutter_easyrefresh 的基本应用,同时使用模拟的方式完成了异步数据加载。
605 0
Flutter 之列表下拉刷新和上拉加载
|
API
为RecyclerView添加下拉刷新功能
在之前的文章中,我们实现了带有header和footer功能的WrapRecyclerView。 现今App中列表的下拉刷新和上拉加载已经是一种习惯了,这两个操作也确实方便很多。 为RecyclerView添加这个功能可以通过多种方法,这里我选用了一种简单的做法。基于pulltorefresh这个库。
162 0
|
Android开发
GridView基于pulltorefresh实现下拉刷新 上拉加载更多功能
GridView基于pulltorefresh实现下拉刷新 上拉加载更多功能
Flutter 21: 图解 ListView 下拉刷新与上拉加载 (三)【RefreshIndicator】
0 基础学习 Flutter,第二十一步:ListView 上拉加载更多与下拉刷新,解决方案三!
5121 0
|
Android开发
Flutter 18: 图解 ListView 下拉刷新与上拉加载 (二)【NotificationListener】
0 基础学习 Flutter,第十八步:ListView 上拉加载更多与下拉刷新,解决方案二!
5000 0