Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created February 15, 2018 20:19
Show Gist options
  • Select an option

  • Save jakehawken/44cbaaadbfa4d8224319afbc70effa46 to your computer and use it in GitHub Desktop.

Select an option

Save jakehawken/44cbaaadbfa4d8224319afbc70effa46 to your computer and use it in GitHub Desktop.
Don't you wish UIRefreshControl used blocks? Don't you wish you could add one to a TableView with one line of code?
import UIKit
class BlockRefreshControl: UIRefreshControl {
private let block: ()->()
init(block: @escaping ()->()) {
self.block = block
super.init()
addTarget(self, action: #selector(BlockRefreshControl.peformBlock), for: .valueChanged)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func peformBlock() {
block()
}
}
extension UITableView {
@discardableResult func addRefreshControl(withAction action: @escaping ()->()) -> UIRefreshControl {
let control = BlockRefreshControl(block: action)
if #available(iOS 10.0, *) {
refreshControl = control
} else {
backgroundView = control
}
return control
}
}
//Example usage
tableView.addRefreshControl { [weak self] in
self?.presenter.forceUpdate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment