您需要向
UITableView
private let refreshControl = UIRefreshControl()
var tableView: UITableView!
然后在
ViewDidLoad
将其作为子视图添加到
tableView
你甚至可以改变它的颜色和文字。
override func viewDidLoad() {
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.addSubview(refreshControl)
}
refreshControl.tintColor = UIColor.blue
refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: [NSAttributedStringKey.foregroundColor : UIColor(red:0/255, green:168/255, blue:225/255, alpha:1.0)])
refreshControl.addTarget(self, action: #selector(refreshData(_:)), for: .valueChanged)
}
当值发生如上所示的变化时,使用选择器函数向其添加一个目标。
@objc private func refreshData(_ sender: Any) {
//load the data from the server and
//parse and put it in an array then reload your table view
}