Ryusuke Fuda's Tech Blog

Softweare Enginier about Web, iOS, Android.

EGOTableViewPullRefresh ios7実装

nanapiのアンサー, Newspicks他いろんなアプリで使われてる引っ張って更新を実装

■ Podfile

pod 'EGOTableViewPullRefresh'

■ storyboardでviewへTableViewをドラッグする。

■ TableViewを選択した状態でビヨーンとViewController.hファイルへ伸ばし、UITableViewのIBOutletをつくる

■ TableViewを選択した状態で右側のOutletsのdelegateからstoryboadのviewの黄色いとこへビヨーンと伸ばす。UITableViewはUIScrollViewを継承しているので、UIScrollViewのdelegateMethodが使える。

■ ViewController.h

#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"

@interface ViewController : UIViewController <EGORefreshTableHeaderDelegate, UITableViewDelegate, UITableViewDataSource>{
	
	EGORefreshTableHeaderView *_refreshHeaderView;
	
	//  Reloading var should really be your tableviews datasource
	//  Putting it here for demo purposes
	BOOL _reloading;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;


@end

■ ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    if (_refreshHeaderView == nil) {
		
		EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - _tableview.bounds.size.height, self.view.frame.size.width, _tableview.bounds.size.height)];
		view.delegate = self;
		[self.tableview addSubview:view];
		_refreshHeaderView = view;
		
	}
	
	//  update the last update date
	[_refreshHeaderView refreshLastUpdatedDate];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}


#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
	
	//  should be calling your tableviews data source model to reload
	//  put here just for demo
	_reloading = YES;
	
}

- (void)doneLoadingTableViewData{
	
	//  model should call this when its done loading
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:_tableview];
	
}


#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	
	[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	
	[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	
}


#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	
	[self reloadTableViewDataSource];
	[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _reloading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}

#pragma mark Memory Management
- (void)viewDidUnload {
	_refreshHeaderView=nil;
}

- (void)dealloc {
	
	_refreshHeaderView = nil;
//    [super dealloc];
}

■ これでRunするとtableview を下へ引っ張ると動きが確認できる。
Pull down to refresh → Release to refresh → Loading

■ iOS7でめり込みを防ぐやり方はこちら参考
http://lab.4cast.co.jp/?p=1145

■ 文字やクルクルの位置をカスタムしたい時いじるファイル
Pods > EGOTableViewPullRefresh > EGORefreshTableHeaderView.m