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

目录
相关文章
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
498 0
|
传感器 机器学习/深度学习 人工智能
苏黎世理工最新!maplab2.0:模块化的多模态建图定位框架
将多传感器模态和深度学习集成到同时定位和mapping(SLAM)系统中是当前研究的重要领域。多模态是在具有挑战性的环境中实现鲁棒性和具有不同传感器设置的异构多机器人系统的互操作性的一块垫脚石。借助maplab 2.0,这个多功能的开源平台,可帮助开发、测试新模块和功能,并将其集成到一个成熟的SLAM系统中。
苏黎世理工最新!maplab2.0:模块化的多模态建图定位框架
|
SQL 关系型数据库 MySQL
mysql用户、权限管理
mysql用户、权限管理
253 0
|
12月前
|
人工智能 自然语言处理 小程序
2023年关键字降本增“笑”,2024年的关键字会是什么呢?
《三潮来袭:2023年科技变革回顾与2024年展望》 2023年,IT行业经历了巨大变革。ChatGPT、AI和降本增效成为关键词。自然语言处理、边缘计算、量子计算等技术取得突破,推动行业发展。2024年,人工智能、云计算、全栈开发将继续引领趋势,移动营销、小程序应用和海外市场拓展将成为新的就业方向。企业将更注重稳定发展,减少试错,提高效率。 未来,持续学习和适应变化将是IT从业者的必备素质。随着全球互联网基础设施的普及,海外市场将为企业带来新的增长点。2024年的关键词可能是“智能化”、“全球化”和“高效化”。
219 5
|
存储 弹性计算 数据库
阿里云服务器租用收费价格参考,弹性裸金属服务器架构云服务器收费价格表
弹性裸金属服务器架构阿里云服务器有计算型弹性裸金属服务器ebmc7、内存型弹性裸金属服务器ebmr7、AMD计算型弹性裸金属服务器ebmc7a、通用型弹性裸金属服务器ebmg6等实例规格可选,不同实例规格的租用收费价格是不一样的,本文为大家汇总了目前基于弹性裸金属服务器架构下的各个实例规格的阿里云服务器收费标准,以供参考。
阿里云服务器租用收费价格参考,弹性裸金属服务器架构云服务器收费价格表
|
运维 监控 Shell
自动化运维之宝:编写高效的Shell脚本
【8月更文挑战第31天】在运维的世界里,Shell脚本是一把瑞士军刀,它让日常任务变得简单而高效。本文将通过浅显易懂的语言和实际案例,带你领略Shell脚本的魅力,并教你如何打造属于自己的自动化工具箱。无论你是初学者还是资深运维,这篇文章都将为你打开一扇窗,让你看到不一样的风景。让我们一起探索Shell脚本的世界吧!
|
人工智能 搜索推荐 前端开发
seo如何优化
木头左,物联网工程师,分享AI工具。本文探讨SEO优化,包括理解基本概念,关键词研究,内容、外部链接和技术优化。关键词研究注重长尾词和竞争度;内容优化要求高质量、结构清晰、定期更新;外部链接要来自高权重源,自然且多样;技术优化涉及URL结构、网站速度、移动友好性和安全性等。记得点赞、收藏和关注哦!
seo如何优化
|
测试技术 数据处理 Python
测试报告导出PDF和excel的方法
测试报告导出PDF和excel的方法
410 1
|
移动开发 双11 Android开发
UITableView顶部突然出现一块空白问题
UITableView顶部突然出现一块空白问题