溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

iOS中UIRefreshControl怎么用

發(fā)布時(shí)間:2021-08-09 11:37:55 來(lái)源:億速云 閱讀:165 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹iOS中UIRefreshControl怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

簡(jiǎn)介:

在展示一些經(jīng)常需要更新的列表時(shí),例如商品列表、聊天列表時(shí),我們需要通過(guò)某種操作來(lái)刷新列表,最常用的便是下拉刷新的方法了,下拉刷新作為iOS的標(biāo)準(zhǔn)控件,即使不實(shí)用第三方庫(kù)也可以容易的實(shí)現(xiàn),這篇文章將向大家講解如何使用UIRefreshControl實(shí)現(xiàn)下拉刷新功能。

UIRefreshControl是iOS6自帶的UITableView下拉刷新控件。iOS6中,UITableViewController已經(jīng)內(nèi)置了UIRefreshControl控件。UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,運(yùn)行時(shí)會(huì)得到如下錯(cuò)誤提示:(即UIRefreshControl只能被UITableViewController管理)  

1. 首先看一下UIRefreshControl的頭文件

NS_CLASS_AVAILABLE_IOS(6_0) @interface UIRefreshControl : UIControl 
- (instancetype)init; 
@property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing; 
// 菊花顏色 
@property (nonatomic, retain) UIColor *tintColor; 
// 下拉刷新文字描述 
@property (nonatomic, retain) NSAttributedString *attributedTitle UI_APPEARANCE_SELECTOR; 
// 開(kāi)始刷新 
- (void)beginRefreshing NS_AVAILABLE_IOS(6_0); 
// 結(jié)束刷新 
- (void)endRefreshing NS_AVAILABLE_IOS(6_0); 
@end

使用方法

     1.目前只對(duì)UITableviewController有用;

     2.目前只能下拉刷新,不能上拉加載;

     3.init或者viewDidLoad中創(chuàng)建UIRefreshControl,設(shè)置文字,顏色等信息;

     4.給UIRefreshControl添加方法,當(dāng)值改變的時(shí)候調(diào)用,方法用于數(shù)據(jù)請(qǐng)求;

     5.該方法中請(qǐng)求數(shù)據(jù)確認(rèn)完成之后,調(diào)用endRefreshing方法,關(guān)閉刷新;

2.示例Demo

#import <UIKit/UIKit.h> 
@interface RefreshTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, retain) UITableView * tableView; 
@property (nonatomic, retain) UIRefreshControl * refreshControl; 
@property (nonatomic, retain) NSMutableArray * dataSource; 
@end
#import <UIKit/UIKit.h> 
@interface RefreshTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, retain) UITableView * tableView; 
@property (nonatomic, retain) UIRefreshControl * refreshControl; 
@property (nonatomic, retain) NSMutableArray * dataSource; 
@end 
#import "RefreshTableViewController.h" 
@interface RefreshTableViewController () 
@end 
 
@implementation RefreshTableViewController 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
  
 _dataSource = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", nil nil]; 
  
 // UITableView 
 _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; 
 _tableView.delegate = self; 
 _tableView.dataSource = self; 
 [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; 
 [self.view addSubview:_tableView]; 
  
 // UIRefreshControl 
 _refreshControl = [[UIRefreshControl alloc] init]; 
 _refreshControl.tintColor = [UIColor redColor]; 
 _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"]; 
 [_refreshControl addTarget:self action:@selector(refreshControlAction) forControlEvents:UIControlEventValueChanged]; 
 [_tableView addSubview:_refreshControl]; 
} 
 
 
- (void) refreshControlAction { 
 if (self.refreshControl.refreshing) { 
  _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"加載中..."]; 
   
  // 1. 遠(yuǎn)程請(qǐng)求數(shù)據(jù) 
  [self requestAPIData]; 
   
  // 2. 結(jié)束刷新 
  [self.refreshControl endRefreshing]; 
   
  // 3. 重新加載數(shù)據(jù) 
  [self.tableView reloadData]; 
 } 
} 
 
 
- (void)requestAPIData { 
 // 模擬遠(yuǎn)程請(qǐng)求所耗費(fèi)的時(shí)間 
 [NSThread sleepForTimeInterval:2]; 
 for (int i = 0; i < 5; i++) { 
  int value = (arc4random() % 100) + 1; 
  [self.dataSource addObject:[NSString stringWithFormat:@"%d", value]]; 
 } 
} 
 
 
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
 return self.dataSource.count; 
} 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
 static NSString * ID = @"UITableViewCell"; 
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID]; 
 if (cell == nil) { 
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 
 } 
  
 cell.textLabel.text = self.dataSource[indexPath.row]; 
 return cell; 
} 
 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
} 
@end

運(yùn)行效果:

iOS中UIRefreshControl怎么用

以上是“iOS中UIRefreshControl怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI