搜索时停用 UIRefreshControl
Deactivate UIRefreshControl while searching
我在 TableView 上实现了一个 UIRefreshControl,另外,我添加了一个带有 searchBar 的 searchController。
搜索和下拉刷新工作得很好,我唯一遇到的问题是如何在 searchController 处于活动状态时停用 "pull to refresh" 功能。
我尝试实施 this 解决方案,但不知何故这对我不起作用。
让你的 viewcontroller 符合 UISearchBarDelegate
class ViewController: UIViewController, UISearchBarDelegate
并实现它的两个方法
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
在每个方法中调用函数来设置和删除 UIRefreshControl
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
完整代码为:
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var refreshControl: UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
self.initRefreshControl()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
@objc func refreshData() {
....
....
self.refreshControl?.endRefreshing()
}
}
这应该有效。
我在 TableView 上实现了一个 UIRefreshControl,另外,我添加了一个带有 searchBar 的 searchController。
搜索和下拉刷新工作得很好,我唯一遇到的问题是如何在 searchController 处于活动状态时停用 "pull to refresh" 功能。
我尝试实施 this 解决方案,但不知何故这对我不起作用。
让你的 viewcontroller 符合 UISearchBarDelegate
class ViewController: UIViewController, UISearchBarDelegate
并实现它的两个方法
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
在每个方法中调用函数来设置和删除 UIRefreshControl
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
完整代码为:
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var refreshControl: UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
self.initRefreshControl()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
@objc func refreshData() {
....
....
self.refreshControl?.endRefreshing()
}
}
这应该有效。