UIRefreshControl 为 UITableViewController 中的一个属性,从以下可以看出, IOS6.0 以上才支持.
@property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);
RootViewController.h
// Copyright (c) 2014年 YouXian. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController
@end
RootViewController.m
// Copyright (c) 2014年 YouXian. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic) NSMutableArray *data;
@end
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数据源
_data = [[NSMutableArray alloc] init];
//初始化refreshControl
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[self.refreshControl addTarget:self
action:@selector(RefreshViewControlEventValueChanged)
forControlEvents:UIControlEventValueChanged];
}
- (void)RefreshViewControlEventValueChanged
{
[self performSelector:@selector(getDataAndStopRefresh)
withObject:nil
afterDelay:2];
}
- (void)getDataAndStopRefresh
{
//处理数据
for (int i = 0; i <= arc4random() % 4; i++)
{
[_data addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]];
}
//结束刷新(重要,只有先处理完了数据源,再执行方法 endRefreshing 让tableView回去)
[self.refreshControl endRefreshing];
//重新加载
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (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];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", _data[indexPath.row]];
return cell;
}
@end
在下拉刷新后,只有执行了方法 [self.refreshControl endRefreshing]; 才能够恢复,简单易用,不过需要在执行这个方法之前处理好一切的数据.