SFPullRefresh (支持 ios5+)
SFPullRefresh 提供了一种非常简单的方法来实现下拉刷新和加载更多,支持自定义刷新或加载更多控件,可以用您自己的视图来定制。
使用 CocoaPods 进行安装
pod 'SFPullRefresh'
使用
基础
将下拉刷新处理程序和加载更多处理程序添加到您的 tableView 或 collectionView 中,当您获取数据时,应调用 [self.table sf_finishLoading] 来结束加载动画,如果没有更多数据,则应调用 [self.table sf_reachEndWithText:@"到达底部"]。
- (void)viewDidLoad
{
...
__weak YourViewController *wkself = self; //you must use wkself to break the retain cycle
[self.table sf_addRefreshHandler:^{
wkself.page = 0;
[wkself loadStrings];
}];
[self.table sf_addLoadMoreHandler:^{
[wkself loadStrings];
}];
...
}
- (void)loadStrings
{
[self requestDataAtPage:self.page success:^(NSArray *strings) {
if (self.table.sf_isRefreshing) {
[self.items removeAllObjects];
}
[self.items addObjectsFromArray:strings];
self.page++;
if (strings.count<10) {
[self.table sf_reachEndWithText:@"加载完毕"];
}
[self.table sf_finishLoading];
} failure:^(NSString *msg) {
[self.items removeAllObjects];
[self.table sf_finishLoading];
}];
}
自定义
您可以使用自己的视图来自定义刷新或加载更多控制。
CustomRefreshControl *customRefreshControl = [[CustomRefreshControl alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
__weak YourViewController *wkself = self; //you must use wkself to break the retain cycle
[self.table sf_addRefreshHandler:^{
wkself.page = 0;
[wkself loadStrings];
} customRefreshControl:customRefreshControl];
自定义视图必须符合协议 SFRefreshControlDelegate 或 SFLoadMoreControlDelegate。
@protocol SFRefreshControlDelegate <NSObject>
- (void)willRefreshWithProgress:(CGFloat)progress;
- (void)beginRefreshing;
- (void)endRefreshing;
@optional
- (void)setTintColor:(UIColor *)tintColor;
@end
@protocol SFLoadMoreControlDelegate <NSObject>
- (void)beginLoading;
- (void)endLoading;
- (void)reachEndWithText:(NSString *)text;
@optional
- (void)setTintColor:(UIColor *)tintColor;
@end
享受使用它吧!