Collection View, Scroll View, Table View在iOS 10都與生俱來支援下拉更新了 !
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屬性。