这个控件内嵌在uitableviewcontroller视图控制器里
使用步骤
1)添加刷新
2)设置颜色
3)设置标题
4)设置监听
5)在监听里获取数据以后停止刷新
//
// MainViewController.m
// refreshcontrol_demo1
//
// Created by mac on 13-10-25.
// Copyright (c) 2013年 mac. All rights reserved.
//
#import "MainViewController.h"
@interfaceMainViewController ()
@property (nonatomic,assign) NSInteger myindex;
@property (nonatomic,strong) NSMutableArray *datalist;
@end
@implementation MainViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.refreshControl = [[UIRefreshControlalloc] init];
//这里如果设置,会一直显示
//self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
self.refreshControl.tintColor = [UIColorredColor];
[self.refreshControladdTarget:selfaction:@selector(myPull) forControlEvents:UIControlEventValueChanged];
self.datalist = [NSMutableArrayarray];
for (NSInteger i=0; i<10; i++) {
self.myindex++;
[self.datalist addObject:@(self.myindex)];
}
[self.tableViewreloadData];
}
- (void)myPull
{
//self.rowcount++;
if (self.refreshControl.refreshing) {
self.refreshControl.attributedTitle = [[NSAttributedStringalloc] initWithString:@"刷新中"];
// 模拟网络加载数据
[selfperformSelector:@selector(doWork) withObject:selfafterDelay:2];
}
}
- (void)doWork
{
NSLog(@"我在工作");
self.myindex++;
[self.datalistaddObject:@(self.myindex)];
[self.refreshControlendRefreshing];
[self.tableViewreloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.datalist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] ;
}
[cell.textLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
return cell;
}
@end