ios下拉刷新上拉加载EGORefresh简单实现
前提下载 EGORefreshTableHeaderView.h EGORefreshTableHeaderView.m
EGORefreshTableFootView.h EGORefreshTableFootView.m 以及8张图片
并拖入工程
在.pch文件里导入
#import "EGORefreshTableHeaderView.h"
#import "EGORefreshTableFootView.h"
在.h文件里
UIScrollViewDelegate,EGORefreshTableHeaderDelegate,EGORefreshTableFootDelegate>
{
EGORefreshTableHeaderView *_refreshHeaderView;
EGORefreshTableFootView* _footerView;
// Reloading var should really be your tableviews datasource
// Putting it here for demo purposes
BOOL _reloading;
BOOL _headerRefresh;
}
@property (nonatomic ,assign) NSInteger pageNumber;
@property(retain,nonatomic)UITableView *tableView;
@property (nonatomic ,retain) NSMutableArray * newdataArray;//新闻数据数组
在.m文件里
viewdidload里
当前页
_pageNumber = 1;
创建刷新视图
_refreshHeaderView = [[EGORefreshTableHeaderView alloc] init];
_refreshHeaderView.frame = CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.tableView.bounds.size.width, self.tableView.bounds.size.height);
_refreshHeaderView.delegate = self;
[self.tableView addSubview:_refreshHeaderView];
_footerView = [[EGORefreshTableFootView alloc] init];
_footerView.frame = CGRectMake(0,0, self.tableView.bounds.size.width, 0);
_footerView.delegate = self;
self.tableView.tableFooterView = _footerView;
[_refreshHeaderView refreshLastUpdatedDate];
然后在外面导入
#pragma mark - 下拉刷新
- (void)finishRefresh{
//LOG_METHOD;
if (NO == _headerRefresh) {
//上拉加载
[self.tableView reloadData];
[_footerView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
}
else{
//下拉刷新 默认刷新
[self.tableView reloadData];
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
}
_reloading = NO;
}
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView *)view{
//LOG_METHOD;
_reloading = YES;
_pageNumber = 1;
[self loadnewdata];
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(finishRefresh) userInfo:nil repeats:NO];
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView *)view{
// NSLog(@"isLoading");
//LOG_METHOD;
return _reloading;
}
- (NSDate *)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView *)view{
//LOG_METHOD;
return [NSDate date];
}
#pragma mark - Footer Refresh delegate methods
- (void)egoRefreshTableFootDidTriggerRefresh:(EGORefreshTableFootView*)view{
//LOG_METHOD;
_reloading = YES;
_pageNumber = _pageNumber+1;
int second = [self.newdataArray count];
int index = second/ 10;
second = second - index*10;
if (second <= 0) {
[self loadnewdata];
}
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(finishRefresh) userInfo:nil repeats:NO];
}
- (BOOL)egoRefreshTableFootDataSourceIsLoading:(EGORefreshTableFootView*)view{
//LOG_METHOD;
return _reloading;
}
- (NSDate*)egoRefreshTableFootDataSourceLastUpdated:(EGORefreshTableFootView*)view{
//LOG_METHOD;
return [NSData data];
}
#pragma mark - scroll delegate methods
//scrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//LOG_METHOD;
CGPoint offset1 = scrollView.contentOffset;
CGRect bounds1 = scrollView.bounds;
UIEdgeInsets inset1 = scrollView.contentInset;
float y1 = offset1.y + bounds1.size.height - inset1.bottom;
if (y1 > self.tableView.frame.size.height) { //判断是下拉刷新 还是上拉加载
_headerRefresh = NO; //上拉加载
[_footerView egoRefreshScrollViewDidScroll:scrollView];
}
else if (y1 < self.tableView.frame.size.height) {
_headerRefresh = YES; //下拉刷新
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
else if (y1 == self.tableView.frame.size.height) {
// DLog(@"%@", flagShuaxin ? @"上拉刷新" : @"下拉刷新");
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//LOG_METHOD;
if (YES == _headerRefresh ) { //下拉刷新
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
else{ //上拉加载更多
[_footerView egoRefreshScrollViewDidEndDragging:scrollView];
}
}
请求数据
-(void)loadnewdata{
NSString *str=[hostname stringByAppendingString:HttpForNewsdataShow];
NSString *astr=[NSString stringWithFormat:@"?ChannelID=%@&PageNums=10&CurrentPage=%d",self.lanmuID,_pageNumber];
NSString *allstrurl=[str stringByAppendingString:astr];
NSLog(@"allstrurl %@",allstrurl);
ASIFormDataRequest *requestlogin = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:allstrurl]];
requestlogin.tag = 300;
requestlogin.delegate = self;
[requestlogin startAsynchronous];
}
请求成功后
if (request.tag == 300)
{
NSString* jsonStr = [request responseString];
NSMutableDictionary * dic1;
dic1 = [jsonStr JSONValue];
NSMutableArray * array = [dic1 objectForKey:@"result"];
if (_pageNumber == 1) {
[self.newdataArray removeAllObjects];
self.newdataArray = [NSMutableArray arrayWithArray:array] ;
self.tableView.tableFooterView.hidden = NO;
}
else
{
[self.newdataArray addObjectsFromArray:array];
}
if ([array count]< 10) {
self.tableView.tableFooterView.hidden = YES;
}
[self.tableView reloadData];
}
运行大功告成!!!!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。