Wednesday, January 9, 2019

How to add refresh controller to table when drag down

1. initinalize a variable:

initialize
private let apRefreshControl = UIRefreshControl()

viewdidload:
// Add Refresh Control to Table View
self.apRefreshControl.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
self.tableView.refreshControl = apRefreshControl
// Add Refresh Control to Table View

if #available(iOS 10.0, *) {
    tableView.refreshControl = apRefreshControl
} else {
    tableView.addSubview(apRefreshControl)
}

//Configure Refresh Control
apRefreshControl.addTarget(self, action: #selector(refreshWeatherData(_:)), for: .valueChanged)


handler
@objc private func refreshWeatherData(_ sender: Any) {
    fetchData()
}
private func fetchData() {
    self.tableView.reloadData()
    self.apRefreshControl.endRefreshing()
}