iOS集成下拉刷新控件 & 实现无感知上拉加载更多

1,567 阅读2分钟

这是我参与8月更文挑战的第5天,活动详情查看: 8月更文挑战” juejin.cn/post/698796…

引言

需求:

1、iOS零售版ERP APP增加支付奖励消息通知在这里插入图片描述 2、通知信息(定时xx点;历史消息可查) 2021-04-29 尊敬的商家,您参与的xxx激励金活动,昨日参与成功10笔,共获得激励金1元! 在这里插入图片描述 在这里插入图片描述

由于消息列表,数据量比较大,为了提升用户体验,需采用分页加载显示数据

I、集成下拉刷新控件

在这里插入图片描述

1.1 定义相关分页属性

  • 分页属性


@property (nonatomic , assign) NSInteger pageNum;//当前页码
@property (nonatomic , assign) NSInteger pageCount;// 总页数
@property (nonatomic , assign) BOOL isfooterRereshing;
// 每页显示数...
  • VM中的事件和数据属性
@property (nonatomic,strong)  NSMutableArray *datas;


@property (nonatomic,strong)  RACSubject *reloadSubject;


@property (nonatomic,strong)  RACSubject *ShowNoviewSubject;


@property (nonatomic,strong)  RACSubject *hidenNoviewSubject;


1.2 监听下拉和上拉事件

  • VC 监听和处理下拉和上拉事件
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];

  • 处理上拉加载数据事件
/** 用于标志下拉动作*/
@property (nonatomic , assign) BOOL isfooterRereshing;

- (void)footerRereshing
{
    self.isfooterRereshing = YES;

    if ((_pageNum + 1) >  _pageCount) {
        
        
        
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
        return;
        
    }
    

    
    
    _pageNum = _pageNum + 1;
    [self doorRequest];
    
    
    
}

  • 处理下拉刷新数据事件
- (void)headerRereshing
{
    self.isfooterRereshing = NO;

    
    
    
    [_doorArr removeAllObjects];// 移除数据,可请求成功之后,再移除
    _pageNum = 1;
    [self doorRequest];
    
    
}


1.3 请求数据的处理

请求成功和失败都要关闭刷新视图

        [weakSelf.vcView.tableView.mj_footer endRefreshing];
        [weakSelf.vcView.tableView.mj_header endRefreshing];
        

完成代码请看原文:kunnan.blog.csdn.net/article/det…

II、iOS实现无感知上拉加载更多

UITableViewDataSourcePrefetching

// this protocol can provide information about cells before they are displayed on screen.

@protocol UITableViewDataSourcePrefetching <NSObject>

@required

// indexPaths are ordered ascending by geometric distance from the table view
- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;

@optional

// indexPaths that previously were considered as candidates for pre-fetching, but were not actually used; may be a subset of the previous call to -tableView:prefetchRowsAtIndexPaths:
- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;

@end


其他实现思路:通过 KVO 去监听 scrollView 的 contentOffset 变化

MJRefreshAutoFooter 有个专门的属性triggerAutomaticallyRefreshPercent 去做自动刷新

#import "MJRefreshFooter.h"

NS_ASSUME_NONNULL_BEGIN

@interface MJRefreshAutoFooter : MJRefreshFooter
/** 是否自动刷新(默认为YES) */
@property (assign, nonatomic, getter=isAutomaticallyRefresh) BOOL automaticallyRefresh;

/** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
@property (assign, nonatomic) CGFloat appearencePercentTriggerAutoRefresh MJRefreshDeprecated("请使用triggerAutomaticallyRefreshPercent属性");

/** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
@property (assign, nonatomic) CGFloat triggerAutomaticallyRefreshPercent;

/** 自动触发次数, 默认为 1, 仅在拖拽 ScrollView 时才生效,
 
 如果为 -1, 则为无限触发
 */
@property (nonatomic) NSInteger autoTriggerTimes;
@end

III 、案例:新浪微博API(获取用户微博数据)

download.csdn.net/download/u0…

  • 集成下拉刷新控件:下拉刷新 HWHomeTableViewController
  • 获取未读消息数: HWHomeTableViewController
  • 封装标题按钮:HWTitleButton

see also

更多内容请关注公众号:iOS逆向