标签:
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
{
NSMutableArray *timeArray;
UIRefreshControl *refresh;
}
@property (strong,nonatomic)NSMutableArray *timeArray;
@property (strong,nonatomic)UIRefreshControl *refresh;
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@end
#import "ViewController.h"
#import "MyTableViewCell.h"
#import "MyTableViewController.h"
@interface ViewController ()
{
NSMutableArray *_imagearray;
}
@end
@implementation ViewController
@synthesize timeArray;
@synthesize refresh;
- (void)viewDidLoad {
[super viewDidLoad];
self.timeArray = [[NSMutableArray alloc]init];
[self setbeginRefreshing];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会得到如下错误提示:(即UIRefreshControl只能被//UITableViewController管理)
//开始设置刷新;
- (void)setbeginRefreshing
{
refresh = [[UIRefreshControl alloc]init];
//刷新图形颜色;
refresh.tintColor = [UIColor redColor];
//刷新标题;
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
//当用户进行下拉刷新操作时,UIRefreshControl 会触发一个UIControlEventValueChanged事件,通过监听这个事件,我们就可以进行类似数据请求的操作了
[refresh addTarget: self action:@selector(refreshTableviewAction:) forControlEvents:UIControlEventValueChanged];
self.refreshControl =refresh;
}
//刷新动作
-(void)refreshTableviewAction:(UIRefreshControl *)refreshs
{
if (refreshs.refreshing) {
refreshs.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
[self performSelector:@selector(refershData) withObject:nil afterDelay:2];
}
}
//刷新数据;
-(void)refershData
{
NSString *syseTime = nil;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//创建时间格式;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
syseTime = [formatter stringFromDate:[NSDate date]];
NSString *lastUpdated = [NSString stringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated] ;
[self.timeArray addObject:syseTime];
//停止刷新,与starRefreshing配对使用;
[self.refreshControl endRefreshing];
//重载数据;
[self.tableview reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.timeArray.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 = [self.timeArray objectAtIndex:indexPath.row];
return cell;
}
标签:
原文地址:http://www.cnblogs.com/lipiao/p/4214533.html