UIRefreshControl 下拉刷新

在自定义 Tableview 中
#import "TableViewController.h"
@interface TableViewController ()
{
NSMutableArray *_dataArray;
}
@end
@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    //创建下拉刷新的控件
    //UIRefreshControl  下拉刷新的类
    UIRefreshControl *ref = [[UIRefreshControl alloc] init];
    //NSAttributedString  属性字符创
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    //给下拉刷新的控件设置标题
    ref.attributedTitle = string;
    //绑定事件
    [ref addTarget:self action:@selector(upDate) forControlEvents:UIControlEventValueChanged];
    //把下拉刷新的控件添加到表上
    self.refreshControl = ref;
    [ref release];
    [string release];
}

-(void)upDate
{
    //当正在刷新时,把标题改为刷新中
    NSAttributedString *string = [[NSAttributedString alloc]  initWithString:@"刷新中"];
    self.refreshControl.attributedTitle  = string;
    [string release];
    //刷新的过程中会有一个耗时的操作
    //在3秒之后执行addData
    [self performSelector:@selector(addData) withObject:nil afterDelay:1];
}

-(void)addData
{
    //刷新结束
    //让刷新的控件停下来
    [self.refreshControl endRefreshing];
    //当刷新结束,改变刷新的标题
    NSAttributedString *string = [[NSAttributedString alloc  initWithString:@"下拉刷新"];
    self.refreshControl.attributedTitle = string;
    [string release];
    //数据加载完毕,接受数据
    [_dataArray addObject:@"新数据"];
    [self.tableView reloadData];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    // Configure the cell...
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}

你可能感兴趣的:(UIRefreshControl 下拉刷新)