iOS UIRefreshControl使用详解

iOS UIRefreshControl使用详解


简介:

          UIRefreshControl是iOS6自带的UITableView下拉刷新控件。iOS6中,UITableViewController已经内置了UIRefreshControl控件。UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会得到如下错误提示:(即UIRefreshControl只能被UITableViewController管理)   


1. 首先看一下UIRefreshControl的头文件

NS_CLASS_AVAILABLE_IOS(6_0) @interface UIRefreshControl : UIControl

- (instancetype)init;

@property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing;

// 菊花颜色
@property (nonatomic, retain) UIColor *tintColor;

// 下拉刷新文字描述
@property (nonatomic, retain) NSAttributedString *attributedTitle UI_APPEARANCE_SELECTOR;

// 开始刷新
- (void)beginRefreshing NS_AVAILABLE_IOS(6_0);

// 结束刷新
- (void)endRefreshing NS_AVAILABLE_IOS(6_0);

@end

使用方法

1.目前只对UITableviewController有用;
2.目前只能下拉刷新,不能上拉加载;
3.init或者viewDidLoad中创建UIRefreshControl,设置文字,颜色等信息;
4.给UIRefreshControl添加方法,当值改变的时候调用,方法用于数据请求;
5.该方法中请求数据确认完成之后,调用endRefreshing方法,关闭刷新;


2.示例Demo

#import 

@interface RefreshTableViewController : UIViewController 

@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain) UIRefreshControl * refreshControl;

@property (nonatomic, retain) NSMutableArray * dataSource;

@end

#import 

@interface RefreshTableViewController : UIViewController 

@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain) UIRefreshControl * refreshControl;

@property (nonatomic, retain) NSMutableArray * dataSource;

@end
#import "RefreshTableViewController.h"

@interface RefreshTableViewController ()

@end

@implementation RefreshTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataSource = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    
    // UITableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.view addSubview:_tableView];
    
    // UIRefreshControl
    _refreshControl = [[UIRefreshControl alloc] init];
    _refreshControl.tintColor = [UIColor redColor];
    _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    [_refreshControl addTarget:self action:@selector(refreshControlAction) forControlEvents:UIControlEventValueChanged];
    [_tableView addSubview:_refreshControl];
}


- (void) refreshControlAction {
    if (self.refreshControl.refreshing) {
        _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"加载中..."];
        
        // 1. 远程请求数据
        [self requestAPIData];
        
        // 2. 结束刷新
        [self.refreshControl endRefreshing];
        
        // 3. 重新加载数据
        [self.tableView reloadData];
    }
}


- (void)requestAPIData {
    // 模拟远程请求所耗费的时间
    [NSThread sleepForTimeInterval:2];
    for (int i = 0; i < 5; i++) {
        int value = (arc4random() % 100) + 1;
        [self.dataSource addObject:[NSString stringWithFormat:@"%d", value]];
    }
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * ID = @"UITableViewCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end


运行效果:
iOS UIRefreshControl使用详解_第1张图片

你可能感兴趣的:(iOS)