Follow the code below for adding pull to refresh to a tableview
| value | |
|---|---|
| iOS version | 11.2 |
| Swift | 4 |
| var refresher : UIRefreshControl! | |
| @IBOutlet weak var tableView: UITableView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| refresher = UIRefreshControl() | |
| // to add text shown below the refresh indicator | |
| refresher.attributedTitle = NSAttributedString(string : "showing below wheel") | |
| refresher.addTarget(self, action: #selector(self.reloadData), for: UIControlEvents.valueChanged) | |
| tableView.addSubview(refresher) | |
| } | |
| @objc func reloadData(){ | |
| //some asynchronous | synchronous task to load data | |
| ... | |
| //on completing the task | |
| if refresher.isRefreshing { | |
| refresher.endRefreshing() | |
| } | |
| } | |