最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中UIRefreshControl的基本使用詳解

 更新時間:2018年04月28日 08:59:20   作者:vbirdbest  
最近在應(yīng)用中用到UIRefreshControl,覺著有必要給大家總結(jié)介紹一下這個控件,所以下面這篇文章主要給大家介紹了關(guān)于iOS中UIRefreshControl的基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下

簡介:

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

UIRefreshControl是iOS6自帶的UITableView下拉刷新控件。iOS6中,UITableViewController已經(jīng)內(nèi)置了UIRefreshControl控件。UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,運行時會得到如下錯誤提示:(即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; 
// 開始刷新 
- (void)beginRefreshing NS_AVAILABLE_IOS(6_0); 
// 結(jié)束刷新 
- (void)endRefreshing NS_AVAILABLE_IOS(6_0); 
@end 

使用方法

     1.目前只對UITableviewController有用;

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

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

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

     5.該方法中請求數(shù)據(jù)確認完成之后,調(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. 遠程請求數(shù)據(jù) 
  [self requestAPIData]; 
   
  // 2. 結(jié)束刷新 
  [self.refreshControl endRefreshing]; 
   
  // 3. 重新加載數(shù)據(jù) 
  [self.tableView reloadData]; 
 } 
} 
 
 
- (void)requestAPIData { 
 // 模擬遠程請求所耗費的時間 
 [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 

運行效果:

關(guān)于UIRefreshControl在使用的一些踩坑指南,大家可以參考這篇文章:http://www.fzitv.net/article/139064.htm

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論

八宿县| 富平县| 皮山县| 旌德县| 红原县| 阜新市| 西充县| 扶风县| 罗山县| 东至县| 湟源县| 镇坪县| 华蓥市| 渑池县| 海伦市| 汉中市| 米易县| 五常市| 博兴县| 辽阳县| 寿宁县| 双城市| 句容市| 玉山县| 米易县| 石门县| 贵定县| 万载县| 桦甸市| 恩施市| 贵州省| 商南县| 中山市| 江安县| 小金县| 绥德县| 新丰县| 肥东县| 裕民县| 万年县| 英吉沙县|