zl程序教程

您现在的位置是:首页 >  其他

当前栏目

使用开源库 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 的基本应用,同时使用模拟的方式完成了异步数据加载。