项目中由于列表显示的数据比较多,我们经常要用到上拉加载更多的功能,写了一个小类实现了此功能,具体使用方法如下:
1. 使用方法:
(1)导入 #import "FJPullTableView.h"
(2)使用 绑定代理FJPullTableViewDelegate, UISCrollViewDelegate
定义变量 FJPullTableView *mytv;
mytv = [[FJPullTableView alloc] initWithFrame:CGRectMake(10, 50, 200, 200)];
mytv.delegate = self;
mytv.dataSource = self;
mytv.pullDelegate = self;
[mytv setIsNeedDisplay:YES];
[self.view addSubview: mytv];
实现代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[mytv fjPullScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[mytv fjPullScrollViewDidEndDragging:scrollView];
}
- (void)beginToFetchData:(FJPullTableView *)tableView {
NSLog(@"start load");
//开始执行加载方法
//模拟执行加载方法
[self performSelector:@selector(doLoad) withObject:nil afterDelay:3];
}
- (void)doLoad {
NSLog(@"load done");
totalCount += 10;
if (totalCount > 30) {
[mytv setIsNeedDisplay:NO];
}
[mytv fjPullScrollViewDataSourceDidFinishedLoading:mytv];
}
注意: 加载更多执行完后
1. 判断是否还要显示上拉刷新提示
[myth setIsNeedDisplay:YES];
2. 调用[mytv fjPullScrollViewDataSourceDidFinishedLoading:mytv];
使用方法比较简单,具体代码实现如下:
#import
@class FJPullTableView;
@protocol FJPullTableViewDelegate
//程序中加载数据的代码在此方法中执行
- (void)beginToFetchData:(FJPullTableView *)tableView;
@end
@interface FJPullTableView : UITableView
@property (nonatomic, assign) id pullDelegate;
//该方法控制tableview是否显示上拉刷新提示,是否执行上拉刷新操作
- (void)setIsNeedDisplay:(BOOL)isNeedToDisplay;
- (void)fjPullScrollViewDidScroll:(UIScrollView *)scrollView;
- (void)fjPullScrollViewDidEndDragging:(UIScrollView *)scrollView;
//更多数据加载完成后需要调用此方法
- (void)fjPullScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;
@end
#import "FJPullTableView.h"
#define PULL_LOAD_MORE @"上拉加载更多"
#define PULL_TO_LOAD @"松开即可加载"
#define PULL_LOADING @"加载中..."
@interface FJPullTableView () {
UILabel *lbLoadingInfo;
BOOL hasLoadingInfo;
BOOL isLoading;
BOOL isNeedDisplay;
UIActivityIndicatorView *indicatorView;
}
@end
@implementation FJPullTableView
@synthesize pullDelegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
isNeedDisplay = YES;
}
return self;
}
//设置tableview是否显示上拉刷新提示
- (void)setIsNeedDisplay:(BOOL)isNeedToDisplay {
isNeedDisplay = isNeedToDisplay;
}
//当tableview向上滚动时调用此方法
- (void)fjPullScrollViewDidScroll:(UIScrollView *)scrollView {
if (isNeedDisplay && !isLoading) {
if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height + 60) {
lbLoadingInfo.text = PULL_TO_LOAD;
} else {
if (!lbLoadingInfo) {
lbLoadingInfo = [[UILabel alloc] initWithFrame:CGRectZero];
lbLoadingInfo.backgroundColor = [UIColor clearColor];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 6.0) {
lbLoadingInfo.textAlignment = NSTextAlignmentCenter;
} else {
lbLoadingInfo.textAlignment = UITextAlignmentCenter;
}
lbLoadingInfo.font = [UIFont systemFontOfSize:12];
lbLoadingInfo.textColor = [UIColor grayColor];
}
if (!hasLoadingInfo) {
lbLoadingInfo.frame = CGRectMake(0, self.contentSize.height,self.frame.size.width, 30);
[self addSubview:lbLoadingInfo];
hasLoadingInfo = YES;
}
lbLoadingInfo.text = PULL_LOAD_MORE;
}
}
}
//当tableview结束拖拽时调用此方法
- (void)fjPullScrollViewDidEndDragging:(UIScrollView *)scrollView {
if (isNeedDisplay && !isLoading) {
if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height + 60) {
isLoading = YES;
//执行加载更多操作
if ([pullDelegate respondsToSelector:@selector(beginToFetchData:)]) {
[self showIndicatorView];
//执行代理方法,进行数据加载
[pullDelegate beginToFetchData:self];
}
}
}
}
//加载数据结束时执行此操作
- (void)fjPullScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
isLoading = NO;
[self hiddenIndicatorView];
[self reloadData];
}
- (void)showIndicatorView {
if (!indicatorView) {
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicatorView.frame = CGRectMake(self.frame.size.width/2 - 50, 5, 20, 20);
}
[indicatorView startAnimating];
lbLoadingInfo.text = PULL_LOADING;
[lbLoadingInfo addSubview:indicatorView];
}
- (void)hiddenIndicatorView {
if (indicatorView) {
[indicatorView stopAnimating];
[indicatorView removeFromSuperview];
}
[lbLoadingInfo removeFromSuperview];
hasLoadingInfo = NO;
}
@end