在iOS 6之後,UITableViewControl添加了一個refreshControl屬性,該屬性保持了UIRefreshControl的一個對象指針。UIRefreshControl就是表視圖實現下拉刷新提供的類,目前該類只能用於表視圖界面。下面我們就來試試該控件的使用。
編寫代碼之前的操作類似於前面幾篇文章。代碼如下:
代碼如下復制代碼#import"ViewController.h"
@interfaceViewController ()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.Logs = [[NSMutableArray alloc]init];//初始化數據
NSDate * date = [[NSDate alloc]init];//初始化日期
[self.Logs addObject:date];//把日期插入數據中
UIRefreshControl * rc = [[UIRefreshControl alloc]init];//初始化UIRefreshControl
rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];//設置下拉框控件標簽
[rc addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];//添加下拉刷新事件
self.refreshControl = rc;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//下拉刷新事件
-(void)refreshAction
{
if(self.refreshControl.refreshing)
{
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加載中"];//設置下拉框控件標簽
NSDate * date = [[NSDate alloc]init];
[self.Logs addObject:date];//每次刷新添加當前日期
[self.refreshControl endRefreshing];//結束刷新
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
[self.tableView reloadData];
}
}
#pragma mark
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[self.Logs count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NSDateFormatter * dateFormat =[[NSDateFormatter alloc]init];//NSDate的轉換類,可將NSDate轉換為其它格式,或者轉換為NSDate格式
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];//設定時間格式
Cell.textLabel.text = [dateFormat stringFromDate:[self.Logs objectAtIndex:indexPath.row]];
Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
returnCell;
}
@end
效果: