Guest User

Untitled

a guest
Feb 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import UIKit
  2.  
  3. public extension UITableView {
  4.  
  5. public func beginRefreshing() {
  6. // Make sure that a refresh control to be shown was actually set on the view
  7. // controller and the it is not already animating. Otherwise there's nothing
  8. // to refresh.
  9. guard let refreshControl = refreshControl, !refreshControl.isRefreshing else {
  10. return
  11. }
  12.  
  13. // Start the refresh animation
  14. refreshControl.beginRefreshing()
  15.  
  16. // Make the refresh control send action to all targets as if a user executed
  17. // a pull to refresh manually
  18. refreshControl.sendActions(for: .valueChanged)
  19.  
  20. // Apply some offset so that the refresh control can actually be seen
  21. let contentOffset = CGPoint(x: 0, y: -refreshControl.frame.height)
  22. setContentOffset(contentOffset, animated: true)
  23. }
  24.  
  25. public func endRefreshing() {
  26. refreshControl?.endRefreshing()
  27. }
  28. }
  29.  
  30. // MARK: - Demo
  31.  
  32. import PlaygroundSupport
  33.  
  34. extension UITableViewCell {
  35. static var identifier: String {
  36. return String(describing: self.self)
  37. }
  38. }
  39.  
  40. class ViewController: UITableViewController {
  41.  
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44.  
  45. let action = #selector(ViewController.refreshControlDidStart(sender:event:))
  46. refreshControl = UIRefreshControl()
  47. refreshControl?.addTarget(self, action: action, for: .valueChanged)
  48.  
  49. tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.identifier)
  50. }
  51.  
  52. func refreshControlDidStart(sender: UIRefreshControl?, event: UIEvent?) {
  53. print(#function)
  54. }
  55.  
  56. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  57. return 100
  58. }
  59.  
  60. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  61. let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.identifier, for: indexPath)
  62. cell.textLabel?.text = "SOMETHING"
  63. return cell
  64. }
  65. }
  66.  
  67. let viewController = ViewController()
  68.  
  69. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  70. viewController.tableView.beginRefreshing()
  71. }
  72.  
  73. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  74. viewController.tableView.endRefreshing()
  75. }
  76.  
  77. PlaygroundPage.current.liveView = viewController
Add Comment
Please, Sign In to add comment