#import "ViewController.h"
#import "EGORefreshTableHeaderView.h"
#import "LoadMoreTableFooterView.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, EGORefreshTableHeaderDelegate, LoadMoreTableFooterDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) EGORefreshTableHeaderView * header;
@property (weak, nonatomic) LoadMoreTableFooterView * footer;
@property (strong, nonatomic) NSMutableArray * array;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//头部
EGORefreshTableHeaderView * header = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, -647, 375, 647)];
self.header = header;
header.delegate = self;
[self.table addSubview:header];
[header refreshLastUpdatedDate];//能使得第一次运行并刷新的时候能显示上次刷新时间,否则只显示@“下拉刷新”
//另一种创建方式
//EGORefreshTableHeaderView *refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, -80, self.view.frame.size.width, 80) arrowImageName:@"exclusive_title_icon.png" textColor:[UIColor redColor]];
// refreshView.backgroundColor = [UIColor blueColor];
//尾部
LoadMoreTableFooterView * footer = [[LoadMoreTableFooterView alloc] init];//frame在viewDidAppear中设置
footer.delegate = self;
self.footer = footer;
[self.table addSubview:footer];
}
//懒加载
- (NSMutableArray *)array
{
if (!_array) {
_array = [NSMutableArray array];
for (int i = 0; i<30; i++) {
[_array addObject:[NSString stringWithFormat:@"lalalalalalalalal %i",i]];
}
}
return _array;
}
//tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identy = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
}
NSString * str = self.array[indexPath.row];
cell.textLabel.text = str;
return cell;
}
//uiscroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView//使得箭头翻转
{
[self.header egoRefreshScrollViewDidScroll:scrollView];
[self.footer egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate//使得释放之后出现转圈
{
[self.header egoRefreshScrollViewDidEndDragging:scrollView];
[self.footer egoRefreshScrollViewDidEndDragging:scrollView];
}
//EGORefreshTableHeaderDelegate的两个代理方法
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view//拉动的时候触发并松手的一瞬间调用
{
NSLog(@"111111");//正在加载的时候拖拽,这个方法默认不会再走,直到这个数据加载完毕之后再走
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:3];
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"刷新完毕");
[self.header egoRefreshScrollViewDataSourceDidFinishedLoading:self.table];
});
});
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view//数据加载完毕之后调用
{
return [NSDate dateWithTimeIntervalSinceNow:181];//多少min之前刷新过,返回的是秒数
}
//LoadMoreTableFooterDelegate的唯一代理方
- (void)loadMoreTableFooterDidTriggerLoadMore:(LoadMoreTableFooterView*)view
{
NSLog(@"22222");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:3];
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"加载更多完毕");
[self.array addObject:@"3333333333"];
[self.array addObject:@"4444444444"];
[self.table reloadData];
[self viewDidAppear:YES];//重设footer的位置
[self.footer egoRefreshScrollViewDataSourceDidFinishedLoading:self.table];
});
});
}
//生命周期方法
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.footer.frame = CGRectMake(0, self.table.contentSize.height, self.view.frame.size.width, 647);
}
@end