BSPreloadTableVew带有预加载功能的tableView

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

  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];
}
复制代码

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

查看所有标签

猜你喜欢:

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

C陷阱与缺陷

C陷阱与缺陷

凯尼格 / 高巍 / 人民邮电出版社 / 2008-2-1 / 30.00元

作者以自己1985年在Bell实验室时发表的一篇论文为基础,结合自己的工作经验扩展成为这本对C程序员具有珍贵价值的经典著作。写作本书的出发点不是要批判C语言,而是要帮助C程序员绕过编程过程中的陷阱和障碍。.. 全书分为8章,分别从词法分析、语法语义、连接、库函数、预处理器、可移植性缺陷等几个方面分析了C编程中可能遇到的问题。最后,作者用一章的篇幅给出了若干具有实用价值的建议。.. 本书......一起来看看 《C陷阱与缺陷》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

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

RGB CMYK 互转工具