BSPreloadTableVew带有预加载功能的tableView

栏目: IOS · 发布时间: 6年前

  1. 一个实现了下拉刷新,上拉加载更多,上拉预加载功能的tableViw
  2. 用的时候只要继承这个tableView即可实现上述功能,可以重写父类实现自定义样式。

效果

BSPreloadTableVew带有预加载功能的tableView

用法

  1. pod 'BSPreloadTableVew'
  2. 代码示例
- (void)viewDidLoad {
[super viewDidLoad];

self.pageSize = 20;
self.pageNum = 1;

[self setUpUI];

__weak typeof(self) weakeSelf = self;
[self.tableView setLoadNewData:^{
__strong typeof(weakeSelf) stongSelf = weakeSelf;
stongSelf.pageNum = 1;
[stongSelf fetchData];
}];
//    [self.tableView setLoadMoreData:^{
//        __strong typeof(weakeSelf) stongSelf = weakeSelf;
//        [stongSelf fetchData];
//    }];

[self.tableView preLoadDataThreshold4WithPageSize:self.pageSize initialPage:self.pageNum curPage:self.pageNum preLoadFetchBlk:^{
__strong typeof(weakeSelf) stongSelf = weakeSelf;
[stongSelf fetchData];
}];

[self showLoading];
[self fetchData];
}
- (void)fetchData{
NSDictionary *dict = @{
@"page":@(self.pageNum),
@"count":@(self.pageSize)
};
__weak typeof(self) weakeSelf = self;
[[BSRequestManager sharedInstance] postWithUrl:@"https://api.apiopen.top/getTangPoetry" args:dict finishBlk:^(NSDictionary * _Nonnull responseObject, NSError * _Nonnull error) {
__strong typeof(weakeSelf) stongSelf = weakeSelf;
[stongSelf.tableView endRefresh];
[stongSelf hideLoading];

if (error) {
[stongSelf.view makeToast:error.localizedDescription];
stongSelf.tableView.isPreLoadRefreshing = NO;
return ;
}

//请求成功
if (stongSelf.pageNum == 1){
NSArray *arrTemp = responseObject[@"result"];
stongSelf.arrMList = [NSMutableArray arrayWithArray:arrTemp];

if (stongSelf.arrMList.count == 0) {
//无数据缺省页

return ;
} else if (stongSelf.arrMList.count < stongSelf.pageSize) {
[stongSelf.tableView hideFooter];
stongSelf.tableView.isPreLoadLastPage = YES;
} else {
[stongSelf.tableView showFooter];
}
[stongSelf.tableView reloadData];
} else {
NSArray *arrTemp = responseObject[@"result"];
[stongSelf.arrMList addObjectsFromArray:arrTemp];

[stongSelf.tableView reloadData];

if (arrTemp.count < stongSelf.pageSize) {
stongSelf.tableView.isPreLoadLastPage = YES;
[stongSelf.tableView hideFooter];
} else {
[stongSelf.tableView showFooter];
}
}
stongSelf.pageNum ++;
stongSelf.tableView.isPreLoadRefreshing = NO;
}];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrMList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;

TableViewCellTest *cellTest = [self.tableView dequeueReusableCellWithIdentifier:@"TableViewCellTest"];
cellTest.data = self.arrMList[indexPath.row];

cell = cellTest;
return cell;
}

- (void)showLoading{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)hideLoading{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

操作系统基础教程

操作系统基础教程

戴维斯 / 第1版 (2006年7月1日) / 2006-7 / 34.0

这是一本关于操作系统基本原理的教科书,其最大特点就是从操作系统的分层概念出发,深入浅出地介绍了操作系统的基本概念和基本框架。本书可以作为高等院校非计算机专业相关课程的教材或参考书,也适合具有高中以上数学基础的计算机用户自学,还可以作为社会上计算机培训机构的教材。对所有想了解计算机操作系统,但又不需要或不打算深入学习其理论和实现细节的读者来说,本书是一本极具价值的入门指导书。一起来看看 《操作系统基础教程》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具