// ViewController.h
// ReferebcceDemo
//
// Created by lengshengren on 13-9-10.
// Copyright (c) 2013年 lengshengren. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *timeArray;
UIRefreshControl *refresh;
}
@property (strong,nonatomic)NSMutableArray *timeArray;
@property (strong,nonatomic)UIRefreshControl *refresh;
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@end
// ViewController.m
// ReferebcceDemo
//
// Created by lengshengren on 13-9-10.
// Copyright (c) 2013年 lengshengren. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize refresh;
@synthesize timeArray;
@synthesize tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
//UIRefreshControl目前只能用于UITableViewController,我写时后用的是viewCtntrol 发现调用不了 self.refreshControl 属性
self.timeArray = [[NSMutableArray alloc]init];
[self setbeginRefreshing];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma 开始刷新函数
- (void)setbeginRefreshing
{
refresh = [[UIRefreshControl alloc]init];
//刷新图形颜色
refresh.tintColor = [UIColor lightGrayColor];
//刷新的标题
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 *syseTiem = nil;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //创建的时间格式
syseTiem = [formatter stringFromDate:[NSDate date]];
NSString *lastUpdated = [NSString stringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated] ;
[self.timeArray addObject:syseTiem];
[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;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end