使用开源库 SVPullToRefresh 实现上拉加载下拉刷新
开源 实现 加载 刷新 上拉 使用
2023-09-14 08:57:59 时间
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; @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 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
Flutter 之列表下拉刷新和上拉加载 在实际的 App 中,下拉刷新和上滑加载更多是非常常见的交互形式。在 Flutter 中,有 flutter_easyrefresh开源插件用于实现下拉刷新和上滑加载更多。本篇介绍了有状态组件和 flutter_easyrefresh 的基本应用,同时使用模拟的方式完成了异步数据加载。
相关文章
- 我的chrome 智能扩展插件copier开源了!!!
- 《CSS 揭秘》作者Lea Verou:我喜欢分享开源的行业文化
- .NET开源免费的功能强大控件库
- dba+开源工具:MHA复刻版,轻松实现MySQL高可用故障转移(附下载)
- 阿里云原生开源大家族加入中科院软件所开源软件供应链点亮计 - 暑期 2021
- 阿里巴巴的云原生应用开源探索与实践
- 曾文旌的私房菜:开源数据库Greenplum Database的实现解析
- SAP产品和3D渲染技术的结合-使用JavaScript的开源3D渲染库实现
- 【原创】开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器
- Atitit 全文检索 pgsql 艾提拉总结 分词 全文索引的实现要靠 PgSQL 的 gin 索引。分词功能 PgSQL 内置了英文、西班牙文等,但中文分词需要借助开源插件 zhparser;
- 【技术种草】介绍一款开源电商网站的购物车添加功能的实现
- 七年音视频开发呕心沥血之作:《Android音视频开发进阶指南》开源分享
- 音视频领域火爆的开源播放器项目
- 开源数据库这么香,为什么我们还要下功夫自研?
- 收集的一些开源的QT地面站项目
- 09_android入门_採用android-async-http开源项目的GET方式或POST方式实现登陆案例
- 【云原生 | 27】Docker部署运行开源消息队列实现RabbitMQ
- PostgreSQL的学习心得和知识总结(一百零三)|深入理解PostgreSQL数据库开源MPP扩展Citus 分布表间连接规则类型JoinRuleType 的实现原理
- PostgreSQL的学习心得和知识总结(九十四)|深入理解PostgreSQL数据库开源MPP扩展Citus DDL命令下发 的实现原理
- PostgreSQL的学习心得和知识总结(九十)|深入理解PostgreSQL数据库开源MPP扩展Citus函数citus_move_shard_placement的实现原理
- PostgreSQL的学习心得和知识总结(八十九)|深入理解PostgreSQL数据库开源MPP扩展Citus再平衡函数rebalance_table_shards的实现原理
- PostgreSQL的学习心得和知识总结(八十五)|深入理解PostgreSQL数据库开源扩展pg_backtrace的使用场景和实现原理
- 【Android开发经验】移动设备的“声波通信/验证”的实现——SinVoice开源项目介绍(一)
- 开源EDR(OSSEC)