Don_Mag

UITableView Refresh

Jul 27th, 2021 (edited)
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.13 KB | None | 0 0
  1.  
  2. class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  3.    
  4.     // assuming we've added a UITableView as a subview
  5.     //  to our UIViewController in Storyboard and connected it
  6.     @IBOutlet var tableView: UITableView!
  7.  
  8.     override func viewDidLoad() {
  9.         super.viewDidLoad()
  10.        
  11.         tableView.dataSource = self
  12.         tableView.delegate = self
  13.        
  14.         // instantiate a UIRefreshControl
  15.         let refreshControl = UIRefreshControl()
  16.         // assign its target action
  17.         refreshControl.addTarget(self, action: #selector(doRefresh(_:)), for: .valueChanged)
  18.         // assign it to the tableView's .refreshControl property
  19.         tableView.refreshControl = refreshControl
  20.        
  21.     }
  22.    
  23.     @objc func doRefresh(_ sender: Any?) -> Void {
  24.         // do what we want for refreshing...
  25.         //  here, we delay for 1-second to simulate getting new data
  26.         DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
  27.             // get a reference to the Refresh Control that called this func
  28.             guard let rc = sender as? UIRefreshControl else { return }
  29.             // done refreshing
  30.             rc.endRefreshing()
  31.         })
  32.     }
  33.  
  34.     // all the other normal tableView stuff
  35.     // ...
  36.  
  37. }
  38.  
Add Comment
Please, Sign In to add comment