本想學(xué)習(xí)了下拉刷新之后自己總結(jié)一下寫篇文章出來的,但覺得學(xué)習(xí)過程中發(fā)現(xiàn)的這篇博文一經(jīng)寫的夠清楚明了了,而且demo也很好,基本跟著走一遍都能學(xué)會了,實(shí)在不覺得自己會寫出什么更好的東西來,所以就直接轉(zhuǎn)載吧,最原始的博文地址已不可考,我看到的也是一篇轉(zhuǎn)載,就不貼原地址啦。
原文的開發(fā)環(huán)境比較老,我是在iOS SDK 8.1 + Xcode 6環(huán)境下開發(fā)的,同樣不用改變什么
原文沒有給出demo工程,這里貼上我自己寫的demo工程:https://github.com/Cloudox/UIRefreshTest
有什么問題歡迎一起討論學(xué)習(xí)~
原文如下:
在IOS6未發(fā)布之前,幾乎都是使用那個(gè)UIRefresh在實(shí)現(xiàn)下拉刷新,甚至有人還是先了上拉的功能,不得不說牛人很多啊??赡苁茿pple意識到了這個(gè)功能的實(shí)用性,在IOS6中增加了下拉刷新,但是上啦還沒有添加。新手自學(xué)了一下這個(gè)下拉刷新的功能,還不錯,分享一下。
首先看一下最終的效果:


注:我使用的是IOS6 SDK + xCode4.5
一、新建一個(gè)工程,基于那么模板沒關(guān)系,這里使用的是Single-view來實(shí)現(xiàn)的。
二、修改ViewController.h文件,由于Apple給出的下拉屬性是在UITableViewController中,所以我們修改我們工程中的類繼承UITableViewController。
使用command+鼠標(biāo)點(diǎn)擊UITableViewController,來到UITableViewController的定義處,我們可以看到如下部分:

在IOS6的版本中添加了UIRefreshControl,使用上面打開UITableViewController同樣的方法,我們來到UIRefreshControl的定義處,可以看到如下的定義:

在UIRefreshControl中有幾個(gè)使用的屬性,我們即將使用到的是attributedTitle 屬性。
查看完API之后,我們開始在ViewController.h文件中修改代碼:
#import <UIKit/UIKit.h>
//修改父類為UITableViewController
@interface UIRefreshViewController : UITableViewController
//定義顯示的個(gè)數(shù)
@property (nonatomic,assign) NSInteger items;
@end
注意這里不需要進(jìn)行代理和數(shù)據(jù)源的書寫,因?yàn)閁ITableViewController中已經(jīng)對這兩個(gè)內(nèi)容進(jìn)行了集成。
三、完成后在ViewController.m文件中初始化顯示的個(gè)數(shù),以及添加為UI添加刷新控件的方法。
- (void)viewDidLoad
{
[super viewDidLoad];
self.items = 0;
[self addRefreshViewController];
}
四、完成后實(shí)現(xiàn)這里的addRefreshViewController方法:
-(void)addRefreshViewController{
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged) forControlEvents:UIControlEventValueChanged];
}
這里我們使用了attributedTitle屬性,這個(gè)屬性就是設(shè)置下拉刷新的文字顯示的。另外,UIRefreshControl響應(yīng)的是ValueChange事件。
五,當(dāng)用戶下拉table的時(shí)候,refreshControl就會響應(yīng)RefreshViewControlEventValueChanged方法,在這個(gè)方法中實(shí)現(xiàn)下拉松開時(shí)所要顯示的文字信息:
-(void)RefreshViewControlEventValueChanged{
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"刷新中..."];
[self performSelector:@selector(loadData) withObject:nil afterDelay:2.0f];
}
這樣在下拉的時(shí)候,refreshControl上的文字就會變成“刷新中。。?!保瑸榱丝吹剿⑿碌男Ч?,設(shè)置了2秒的延后執(zhí)行l(wèi)oadData,對用戶來說是一個(gè)友好性,另外Apple的下拉還帶有”粘性“類似的效果,蠻有意思。
六、編寫loadData方法,實(shí)現(xiàn)界面數(shù)據(jù)的刷新。同時(shí)refreshControl置為”下拉刷新“狀態(tài)。
-(void)loadData{
[self.refreshControl endRefreshing];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
self.items++;
[self.tableView reloadData];
}
七、完成這些之后,還不能出現(xiàn)界面,需要我們實(shí)現(xiàn)TableView的那些delegate方法和dataSource方法:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return self.items;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"Evolution.cc & %d",indexPath.row];
return cell;
}