#import
//刷新头视图
#import "EGORefreshTableHeaderView.h"
//下拉刷新数据
#import "LoadMoreTableFooterView.h"
//添加EGORefreshTableHeaderDelegate,LoadMoreTableFooterDelegate
@interface ViewController :UIViewController<</span>UITableViewDataSource,UITableViewDelegate,EGORefreshTableHeaderDelegate,LoadMoreTableFooterDelegate>
{
UITableView *tableView;
}
//相当于setTableView;
@property (nonatomic,strong)UITableView *tableView;
@end
#import "ViewController.h"
@interface ViewController ()
{
EGORefreshTableHeaderView *egoRefreshTableHeaderView;
BOOL isRefreshing;
LoadMoreTableFooterView *loadMoreTableFooterView;
BOOL isLoadMoreing;
int dataRows;
}
@end
@implementation ViewController
@synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
isRefreshing = NO;
dataRows = 20;
CGRect rect = self.view.bounds;
//添加64是为了留给一段空白刷新显示
rect.origin.y = self.view.bounds.origin.y+64;
self.tableView = [[UITableView alloc]initWithFrame:rectstyle:UITableViewStylePlain];
//添加数据库
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource =self;
//初始化上拉刷新
if (egoRefreshTableHeaderView==nil) {
egoRefreshTableHeaderView = [[EGORefreshTableHeaderViewalloc]initWithFrame:CGRectMake(0, 0.0f - self.tableView.bounds.size.height,self.view.frame.size.width, self.tableView.bounds.size.height )];
//添加到self.tableView
[self.tableView addSubview:egoRefreshTableHeaderView];
}
//刷新最新日期
[egoRefreshTableHeaderView refreshLastUpdatedDate];
if (loadMoreTableFooterView==nil) {
//下拉刷新初始化
loadMoreTableFooterView = [[LoadMoreTableFooterViewalloc]initWithFrame:CGRectMake(0, self.tableView.bounds.size.height,self.view.frame.size.width, self.tableView.bounds.size.height)];
loadMoreTableFooterView.delegate = self;
//添加到 self.tableView
[self.tableView addSubview:loadMoreTableFooterView];
}
//加载数据 加载数据
[self reloadData];
}
- (void)reloadData
{
[self.tableView reloadData];
loadMoreTableFooterView.frame = CGRectMake(0,self.tableView.contentSize.height, self.view.frame.size.width,self.tableView.bounds.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return dataRows;
}
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableViewt cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewt dequeueReusableCellWithIdentifier:@"nihaoma"];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nihaoma"];
}
cell.textLabel.text = @(indexPath.row).description;
return cell;
}
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[egoRefreshTableHeaderView egoRefreshScrollViewDidScroll:scrollView];
[loadMoreTableFooterView loadMoreScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[egoRefreshTableHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
[loadMoreTableFooterView loadMoreScrollViewDidEndDragging:scrollView];
}
#pragma mark EGORefreshTableHeaderDelegate
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
isRefreshing = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
sleep(3);
dataRows=20;
dispatch_sync(dispatch_get_main_queue(), ^{
isRefreshing = NO;
[self reloadData];
[egoRefreshTableHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
});
});
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
return isRefreshing;
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
return [NSDate date];
}
#pragma mark LoadMoreTableFooterDelegate
- (void)loadMoreTableFooterDidTriggerLoadMore:(LoadMoreTableFooterView*)view
{
isRefreshing = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
sleep(3);
dataRows += 20;
dispatch_sync(dispatch_get_main_queue(), ^{
isLoadMoreing = NO;
[self reloadData];
[loadMoreTableFooterView loadMoreScrollViewDataSourceDidFinishedLoading:self.tableView];
});
});
}
- (BOOL)loadMoreTableFooterDataSourceIsLoading:(LoadMoreTableFooterView*)view
{
return isLoadMoreing;
}
@end