想要下拉刷新表格數(shù)據(jù)数冬,上拉加載新數(shù)據(jù),網(wǎng)上有許多第三方的實(shí)現(xiàn)類搀庶。
而如果僅僅需要實(shí)現(xiàn)下拉刷新數(shù)據(jù)的話拐纱,那么使用 UIRefreshControl 就足夠了,簡單有好用哥倔。
1秸架,UIRefreshControl 的使用步驟:
(1)創(chuàng)建 UIRefreshControl,并設(shè)置文字咆蒿,顏色等信息东抹。
(2)將 UIRefreshControl 添加到tableview視圖中。
(3)給 UIRefreshControl 添加方法沃测,當(dāng)值改變的時(shí)候調(diào)用缭黔,用于數(shù)據(jù)請求刷新。
(4)請求數(shù)據(jù)確認(rèn)完成之后蒂破,調(diào)用endRefreshing方法馏谨,關(guān)閉刷新。
2附迷,效果圖如下
原文:Swift - 下拉刷新數(shù)據(jù)的功能實(shí)現(xiàn)(使用UIRefreshControl) 原文:Swift - 下拉刷新數(shù)據(jù)的功能實(shí)現(xiàn)(使用UIRefreshControl) 原文:Swift - 下拉刷新數(shù)據(jù)的功能實(shí)現(xiàn)(使用UIRefreshControl)
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ī)添加5條新數(shù)據(jù)(時(shí)間是當(dāng)前時(shí)間)
for _ in 0..<5 {
let atricle = HanggeArticle(title: "新聞標(biāo)題\(Int(arc4random()%1000))",
createDate: NSDate())
self.dataArray.append(atricle)
}
self.newsTableView.reloadData()
self.refreshControl.endRefreshing()
}
原文出自:www.hangge.com? 轉(zhuǎn)載請保留原文鏈接:http://www.hangge.com/blog/cache/detail_934.html