下拉刷新是重新刷新表視圖或列表,以便重新加載數據,這種形式普遍用於挪動平台,置信大家關於此也是十分熟習的,那麼IOS是如何做到的下拉刷新呢?
在IOS 6之後,UITableViewControl添加了一個refreshControl屬性,該屬性堅持了UIRefreshControl的一個對象指針。UIRefreshControl就是表視圖完成下拉刷新提供的類,目前該類只能用於表視圖界面。上面我們就來試試該控件的運用。
編寫代碼之前的操作相似於後面幾篇文章。代碼如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
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 {
[super didReceiveMemoryWarning];
// 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
{
return 1;
}
-(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;
return Cell;
}
@end
效果:



以上所述是本站給大家引見的IOS表視圖之下拉刷新控件功用的完成辦法,希望對大家有所協助,假如大家有任何疑問請給我留言,本站會及時回復大家的。在此也十分感激大家對本站網站的支持!
【iOS表視圖之下拉刷新控件功用的完成辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!