Created
February 15, 2018 20:19
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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