欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swift開(kāi)發(fā)之使用UIRefreshControl實(shí)現(xiàn)下拉刷新數(shù)據(jù)及uirefreshcontrol使用

 更新時(shí)間:2015年11月10日 11:39:22   投稿:mrr  
本文給大家介紹使用UIRefreshControl實(shí)現(xiàn)下拉刷新數(shù)據(jù),及UIRefreshControl的使用步驟,對(duì)本文感興趣的朋友一起學(xué)習(xí)吧

想要下拉刷新表格數(shù)據(jù),上拉加載新數(shù)據(jù),網(wǎng)上有許多第三方的實(shí)現(xiàn)類(lèi)。

而如果僅僅需要實(shí)現(xiàn)下拉刷新數(shù)據(jù)的話,那么使用 UIRefreshControl 就足夠了,簡(jiǎn)單有好用。

1.UIRefreshControl 的使用步驟:

(1)創(chuàng)建 UIRefreshControl,并設(shè)置文字,顏色等信息。
(2)將 UIRefreshControl 添加到tableview視圖中。
(3)給 UIRefreshControl 添加方法,當(dāng)值改變的時(shí)候調(diào)用,用于數(shù)據(jù)請(qǐng)求刷新。
(4)請(qǐng)求數(shù)據(jù)確認(rèn)完成之后,調(diào)用endRefreshing方法,關(guān)閉刷新。

2.效果圖如下

 

3.代碼如下

import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
 //新聞列表
 @IBOutlet weak var newsTableView: UITableView!
 //新聞數(shù)組集合
 var dataArray:[HanggeArticle] = [HanggeArticle]()
 //拉刷新控制器
 var refreshControl = UIRefreshControl()
 override func viewDidLoad() {
  super.viewDidLoad()
  self.automaticallyAdjustsScrollViewInsets = false
  //添加刷新
  refreshControl.addTarget(self, action: "refreshData",
   forControlEvents: UIControlEvents.ValueChanged)
  refreshControl.attributedTitle = NSAttributedString(string: "下拉刷新數(shù)據(jù)")
  newsTableView.addSubview(refreshControl)
  refreshData()
 }
 // 刷新數(shù)據(jù)
 func refreshData() {
  //移除老數(shù)據(jù)
  self.dataArray.removeAll()
  //隨機(jī)添加條新數(shù)據(jù)(時(shí)間是當(dāng)前時(shí)間)
  for _ in ..< {
   let atricle = HanggeArticle(title: "新聞標(biāo)題\(Int(arcrandom()%))",
    createDate: NSDate())
   self.dataArray.append(atricle)
  }
  self.newsTableView.reloadData()
  self.refreshControl.endRefreshing()
 }
 // 返回記錄數(shù)
 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return dataArray.count;
 }
 // 返回單元格內(nèi)容
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
  -> UITableViewCell {
  let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
   reuseIdentifier: "myCell")
  //設(shè)置單元格標(biāo)題
  let atricle: HanggeArticle = dataArray[indexPath.row] as HanggeArticle
  cell.textLabel?.text = atricle.title
  //設(shè)置單元格副標(biāo)題
  let dateFormatter = NSDateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  let str = dateFormatter.stringFromDate(atricle.createDate)
  cell.detailTextLabel?.text = str
  return cell;
 }
 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
 }
}
//新聞結(jié)構(gòu)體
struct HanggeArticle {
 var title:String
 var createDate:NSDate
}

PS:UIRefreshControl的使用

1、使用范圍

如果你裝了xcode_4.5_developer_preview,那么在UITableViewController.h文件中你會(huì)看到,UITableViewController里面有如下聲明,說(shuō)明UITableViewController已經(jīng)內(nèi)置了UIRefreshControl控件

復(fù)制代碼 代碼如下:

@property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0); 

【注】:UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,運(yùn)行時(shí)會(huì)得到如下錯(cuò)誤提示:(即UIRefreshControl只能被UITableViewController管理)

2012-06-15 14:34:34.908 DevDivUIRefreshControl[722:10103] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIRefreshControl may only be managed by a UITableViewController' 
*** First throw call stack: 
(0x186fd72 0x1066e51 0x186fb4b 0x55a559 0x57238 0x5d482 0x55ad2 0x2ebb 0xeb2a3 0xeb30e 0x10b7e9 0x10b624 0x109aef 0x10999c 0x107adc 0x1082c6 0xecf24 0xed1e0 0xee084 0x5645c 0x5cf31 0x55ad2 0x4131d 0x414f6 0x4168c 0x49871 0x10a90 0x1196a 0x222be 0x22f9f 0x153fd 0x17ccf39 0x17ccc10 0x17e5da5 0x17e5b12 0x1816b46 0x1815ed4 0x1815dab 0x1128f 0x12e71 0x29fd 0x2925) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

2、如何使用

    a)初始化

如何在UITableViewController 中使用UIRefreshControl呢,在上面給出的代碼附件中,你可以很詳細(xì)的知道,這里介紹一下關(guān)鍵的部分:

self.refreshControl = [[UIRefreshControl alloc]init]; 
 // self.refreshControl.tintColor = [UIColor blueColor]; 
 self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; 
 [self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged) forControlEvents:UIControlEventValueChanged]; 

如上面看到的代碼,雖然UITableViewController已經(jīng)聲明了UIRefreshControl,但是貌似還沒(méi)有初始化,所以需要我們自己初始化。很神奇,初始化的時(shí)候并不需要給它指定frame,UITableViewController會(huì)為我們進(jìn)行管理。遺憾的時(shí)目前只看到下拉刷新功能,上拉刷新還沒(méi)有,估計(jì)在最終版里面蘋(píng)果會(huì)考慮加入上拉刷新功能。
我們還可以給UIRefreshControl設(shè)置tintColor和attributedTitle。

 b)下拉刷新事件監(jiān)聽(tīng)

當(dāng)用戶(hù)進(jìn)行下拉刷新操作時(shí),UIRefreshControl 會(huì)觸發(fā)一個(gè)UIControlEventValueChanged事件,通過(guò)監(jiān)聽(tīng)這個(gè)事件,我們就可以進(jìn)行類(lèi)似數(shù)據(jù)請(qǐng)求的操作了。如下代碼:

復(fù)制代碼 代碼如下:

[self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged)

c)進(jìn)行數(shù)據(jù)請(qǐng)求

在示例中,為了演示數(shù)據(jù)請(qǐng)求,我簡(jiǎn)單的做了一個(gè)延時(shí)處理,2秒鐘后,調(diào)用handleData

復(fù)制代碼 代碼如下:

[self performSelector:@selector(handleData) withObject:nil afterDelay:2]; 

在handleData里面,就表示已經(jīng)請(qǐng)求到了數(shù)據(jù),在此進(jìn)行UI更新即可。也需要注意的是,我們調(diào)用UIRefreshControl 的endRefreshing方法,表示刷新結(jié)束,讓UIRefreshControl更新顯示。

- (void) handleData 
{ 
 NSLog(@"refreshed"); 
 [self.refreshControl endRefreshing]; 
 self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; 
 
 self.count++; 
 [self.tableView reloadData]; 
} 

3、官方頭文件

下面是sdk中UIRefreshControl的聲明,想必看了下面的代碼,你已經(jīng)知道如何使用了。

// 
// UIRefreshControl.h 
// UIKit 
// 
// Copyright 2012 Apple Inc. All rights reserved. 
// 
#import <Foundation/Foundation.h> 
#import <UIKit/UIControl.h> 
#import <UIKit/UIKitDefines.h> 
NS_CLASS_AVAILABLE_IOS(6_0) @interface UIRefreshControl : UIControl 
/* The designated initializer 
 * This initializes a UIRefreshControl with a default height and width. 
 * Once assigned to a UITableViewController, the frame of the control is managed automatically. 
 * When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event. 
 */ 
- (id)init; 
@property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing; 
@property (nonatomic, retain) UIColor *tintColor UI_APPEARANCE_SELECTOR; 
@property (n

相關(guān)文章

  • Swift 字符串類(lèi)型及常用方法詳解總結(jié)

    Swift 字符串類(lèi)型及常用方法詳解總結(jié)

    Swift 字符串是一系列字符的集合。例如 "Hello, World!" 這樣的有序的字符類(lèi)型的值的集合,它的數(shù)據(jù)類(lèi)型為 String,接下來(lái)文章將詳細(xì)探討
    2021-11-11
  • 理解二叉堆數(shù)據(jù)結(jié)構(gòu)及Swift的堆排序算法實(shí)現(xiàn)示例

    理解二叉堆數(shù)據(jù)結(jié)構(gòu)及Swift的堆排序算法實(shí)現(xiàn)示例

    二插堆即是完全二叉樹(shù),對(duì)于排序可以按構(gòu)建最大堆或最小堆的方式來(lái)實(shí)現(xiàn),這里我們就來(lái)共同理解二叉堆數(shù)據(jù)結(jié)構(gòu)及Swift的堆排序算法實(shí)現(xiàn)示例
    2016-07-07
  • Swift縮放并填充圖片功能的實(shí)現(xiàn)

    Swift縮放并填充圖片功能的實(shí)現(xiàn)

    最近有一個(gè)需求,就是將圖片先等比例縮放到指定大小,然后將空余出來(lái)空間填充為黑色,返回指定大小的圖片。本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

    Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • swift版webview加載網(wǎng)頁(yè)進(jìn)度條效果

    swift版webview加載網(wǎng)頁(yè)進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)webview加載網(wǎng)頁(yè)進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Swift中風(fēng)味各異的類(lèi)型擦除實(shí)例詳解

    Swift中風(fēng)味各異的類(lèi)型擦除實(shí)例詳解

    你也許曾聽(tīng)過(guò)類(lèi)型擦除,甚至也使用過(guò)標(biāo)準(zhǔn)庫(kù)提供的類(lèi)型擦除類(lèi)型如 AnySequence,下面這篇文章主要給大家介紹了關(guān)于Swift中風(fēng)味各異的類(lèi)型擦除的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 深入理解Swift中的變量與常量

    深入理解Swift中的變量與常量

    本文主要是介紹Swift中最常用的常量和變量,將從“變量常量的定義”、"如何聲明變量常量"、“變量和常量的命名”,"變量常量的本質(zhì)區(qū)別"四個(gè)方面入手,重點(diǎn)介紹變量和常量的使用以及區(qū)別,希望大家在閱讀完本文后都可以熟練使用它們。有需要的朋友們下面來(lái)一起學(xué)習(xí)吧。
    2017-01-01
  • Swift實(shí)現(xiàn)堆排序算法的代碼示例

    Swift實(shí)現(xiàn)堆排序算法的代碼示例

    堆排序(HeapSort)是一樹(shù)形選擇排序,堆排序的時(shí)間復(fù)雜度O(nlogn),這里我們來(lái)看一下Swift實(shí)現(xiàn)基堆排序算法的代碼示例,首先對(duì)堆排序算法的基本概念作一個(gè)了解:
    2016-06-06
  • Swift 3.0基礎(chǔ)學(xué)習(xí)之下標(biāo)

    Swift 3.0基礎(chǔ)學(xué)習(xí)之下標(biāo)

    這篇文章主要介紹了Swift 3.0基礎(chǔ)學(xué)習(xí)之下標(biāo)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用swift具有一定的參考價(jià)值,需要的朋友下面來(lái)一起看看吧。
    2017-03-03
  • 在Swift中使用Cocoa的現(xiàn)有設(shè)計(jì)模式介紹

    在Swift中使用Cocoa的現(xiàn)有設(shè)計(jì)模式介紹

    這篇文章主要介紹了在Swift中使用Cocoa的現(xiàn)有設(shè)計(jì)模式介紹,Cocoa是蘋(píng)果公司為Mac OS X所創(chuàng)建的原生面向?qū)ο蟮腁PI,是Mac OS X上五大API之一,需要的朋友可以參考下
    2014-07-07

最新評(píng)論