EGORefreshTableHeaderView与LoadMoreTableFooterView的使用

来源:互联网 发布:小米盒子怎么连接网络 编辑:程序博客网 时间:2024/05/21 06:35


#import "ViewController.h"

#import "EGORefreshTableHeaderView.h"

#import "LoadMoreTableFooterView.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,EGORefreshTableHeaderDelegate, LoadMoreTableFooterDelegate>

@property (weak, nonatomic) IBOutletUITableView *table;

@property (weak,nonatomic) EGORefreshTableHeaderView * header;

@property (weak,nonatomic) LoadMoreTableFooterView * footer;


@property (strong,nonatomic) NSMutableArray * array;


@end


@implementation ViewController



- (void)viewDidLoad {

    [superviewDidLoad];

    

   //头部

    EGORefreshTableHeaderView * header = [[EGORefreshTableHeaderViewalloc] initWithFrame:CGRectMake(0, -647,375, 647)];

   self.header = header;

    header.delegate =self;

    [self.tableaddSubview: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  = [[LoadMoreTableFooterViewalloc] init];//frameviewDidAppear中设置

    footer.delegate =self;

   self.footer = footer;

    [self.tableaddSubview:footer];

}


//懒加载

- (NSMutableArray *)array

{

   if (!_array) {

       _array = [NSMutableArrayarray];

       for (int i =0; i<30; i++) {

            [_arrayaddObject:[NSStringstringWithFormat:@"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 = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];

    }

   NSString * str = self.array[indexPath.row];

    cell.textLabel.text = str;

   return cell;

}



//uiscroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView//使得箭头翻转

{

    [self.headeregoRefreshScrollViewDidScroll:scrollView];

    [self.footeregoRefreshScrollViewDidScroll:scrollView];

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate//使得释放之后出现转圈

{

    [self.headeregoRefreshScrollViewDidEndDragging:scrollView];

    [self.footeregoRefreshScrollViewDidEndDragging:scrollView];

}



//EGORefreshTableHeaderDelegate的两个代理方法

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view//拉动的时候触发并松手的一瞬间调用

{

    NSLog(@"111111");//正在加载的时候拖拽,这个方法默认不会再走,直到这个数据加载完毕之后再走

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        [NSThreadsleepForTimeInterval:3];

        

        dispatch_sync(dispatch_get_main_queue(), ^{

           NSLog(@"刷新完毕");

            [self.headeregoRefreshScrollViewDataSourceDidFinishedLoading:self.table];

        });

        

    });

}


- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view//数据加载完毕之后调用

{

    return [NSDatedateWithTimeIntervalSinceNow:181];//多少min之前刷新过,返回的是秒数

}


//LoadMoreTableFooterDelegate的唯一代理方

- (void)loadMoreTableFooterDidTriggerLoadMore:(LoadMoreTableFooterView*)view

{

    NSLog(@"22222");

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        [NSThreadsleepForTimeInterval:3];

        

        dispatch_sync(dispatch_get_main_queue(), ^{

           NSLog(@"加载更多完毕");

            [self.arrayaddObject:@"3333333333"];

            [self.arrayaddObject:@"4444444444"];

            [self.tablereloadData];

            [selfviewDidAppear:YES];//重设footer的位置

            [self.footeregoRefreshScrollViewDataSourceDidFinishedLoading:self.table];

        });

        

    });

}


//生命周期方法

- (void)viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

    

    self.footer.frame =CGRectMake(0,self.table.contentSize.height,self.view.frame.size.width,647);

}

@end


0 0