Collection View, Scroll View, Table View在iOS 10都與生俱來支援下拉更新了 !

彼得潘的 iOS App Neverland
2 min readJun 21, 2016

在iOS 10,Collection View, Scroll View, Table View都與生俱來支援UIRefreshControl物件,只要三個步驟即可實現下拉更新功能:

1. 生成UIRefreshControl物件。

2. 設定UIRefreshControl物件在下拉更新時觸發的function。

3. 將UIRefreshControl物件指定到Collection View, Scroll View或Table View的refreshControl屬性。

實例:

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action:
#selector(ViewController.refreshData(refreshControl:)) ,
for: .valueChanged)
self.movieTableView.refreshControl = refreshControl

到底這refreshControl屬性是哪裡來的呢? 其實它來自protocol UIRefreshControlHosting。

public protocol UIRefreshControlHosting : NSObjectProtocol {     public var refreshControl: UIRefreshControl? { get set }}

UIScrollView遵從protocol UIRefreshControlHosting,而UITableView和UICollectionView皆繼承UIScrollView,因此讓Collection View, Scroll View, Table View都獲得了refreshControl屬性。

--

--

彼得潘的 iOS App Neverland

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com