UIRefreshControl 爲 UITableViewController 中的一個屬性,從如下能夠看出, IOS6.0 以上才支持.dom
@property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);
RootViewController.hatom
// Copyright (c) 2014年 YouXian. All rights reserved. // #import <UIKit/UIKit.h> @interface RootViewController : UITableViewController @end
RootViewController.mspa
// 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]; 纔可以恢復,簡單易用,不過須要在執行這個方法以前處理好一切的數據.code