下拉刷新的实现

使用EGOTableViewPullRefresh开源类库需要导入QuartzCore.framework框架

//MainViewController.h

#import <UIKit/UIKit.h>

#import "EGORefreshTableHeaderView.h"


@interface MainViewController : UIViewController

<UITableViewDelegate,UITableViewDataSource,EGORefreshTableHeaderDelegate>

{

    UITableView *myTableView;

    

    BOOL shouldLoading;

    BOOL isLoading;

    

    EGORefreshTableHeaderView *refreshView;

    BOOL isRefresh;

    

    NSMutableArray *dataArray;

}


//MainViewController.m


#import "MainViewController.h"


@interface MainViewController()

- (void)loading;

@end


@implementation MainViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    dataArray = [[NSMutableArray alloc] initWithCapacity:0];

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

        [dataArray addObject:@"好好学习"];

    }


    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];

    myTableView.delegate = self;

    myTableView.dataSource = self;

    

    [self.view addSubview:myTableView];

    [myTableView release];

    

    refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0 - myTableView.bounds.size.height, myTableView.bounds.size.width, myTableView.bounds.size.height)];

    refreshView.delegate = self;

    

    [myTableView addSubview:refreshView];

    //刷新一次

    [refreshView refreshLastUpdatedDate];


}


#pragma mark - UITableViewDataSource协议中的方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [dataArray count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"myCell"];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease];

    }

    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];

 

    return cell;

}


#pragma mark - EGORefreshTableHeaderDelegate协议中的方法


- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view

{

    //开始更新代码

    isRefresh = YES;

    //实现更新代码

    [self loading];

}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view

{

    return isRefresh;

}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view

{

    return [NSDate date];

}


#pragma mark - UIScrollViewDelegate协议中的方法


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //通知一下refreshView (向上箭头)

    [refreshView egoRefreshScrollViewDidScroll:scrollView];

}


//手指离开屏幕


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{    

    CGFloat aHeight = scrollView.frame.size.height;

    if (scrollView.contentOffset.y + aHeight > scrollView.contentSize.height) {

        shouldLoading = YES;

    }else{

        shouldLoading = NO;

    }

    

    // 加载  UIActivityIndicatorView

    [refreshView egoRefreshScrollViewDidEndDragging:scrollView];


//滚动已经停止

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    if (shouldLoading) {

        [self loading];

    }

}



#pragma mark - 更新代码

- (void)loading

{

    if (isLoading) {

        return;

    }

    isLoading = YES;

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

        [dataArray insertObject:@"天天向上" atIndex:i];

    }

    [NSThread detachNewThreadSelector:@selector(startBackgroundJob:) toTarget:self withObject:nil];

}


- (void)startBackgroundJob:(id)arg

{

    //另开线程,启动加载任务

    sleep(3);

    

    [self performSelectorOnMainThread:@selector(finishDownload:) withObject:nil waitUntilDone:NO];

}


- (void)finishDownload:(id)arg

{

    isLoading = NO;

    isRefresh = NO;

  

    // 移除 UIAcitivityIndicator

    [refreshView egoRefreshScrollViewDataSourceDidFinishedLoading:myTableView];

    [myTableView reloadData];

}


@end






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值