【问题标题】:Swift - Disable refreshControl while searchingSwift - 搜索时禁用 refreshControl
【发布时间】:2015-06-01 19:22:14
【问题描述】:
在搜索过程中,我想禁用拉动刷新机制。所以我禁用了刷新控件并将其删除。但是当下拉刷新时会调用 beginRefresh 方法,并且单元格会保持关闭 2 秒,就像有刷新一样。
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
resultSearchController.searchBar.selectedScopeButtonIndex = 0
refreshControl!.enabled = false
refreshControl?.removeFromSuperview()
return true
}
【问题讨论】:
标签:
uitableview
swift
uirefreshcontrol
【解决方案1】:
如果要在 UITableView 顶部创建或删除刷新控件,请使用这两个函数。
func createRefreshControl() {
// Create RefreshControl and add to tableView
self.refreshControl = UIRefreshControl()
self.refreshControl!.attributedTitle = NSAttributedString(string: " ↓ Refresh ↓ ")
self.refreshControl!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
}
func deleteRefreshControl() {
// delete RefreshControl
self.refreshControl = nil
}
【解决方案2】:
搜索时再检查refreshControl是否有superView,并从superView中删除搜索结束后再次添加,条件如下:
if self.refreshControl.isDescendant(of: self.tblView) {
self.refreshControl.removeFromSuperview()
}
【解决方案3】:
试试这个代码,它对我有用:
var refresh : UIRefreshControl!
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
print("START TABBAR")
if #available(iOS 10.0, *) {
if let refresh = self.tableView.refreshControl {
if refresh.isDescendant(of: self.tableView) {
self.tableView.refreshControl?.removeFromSuperview()
}
}
} else {
for subview in self.tableView.subviews {
if let refresh = subview as? UIRefreshControl {
refresh.removeFromSuperview()
}
}
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("FINISH SEARCH")
self.refresh = UIRefreshControl()
self.refresh.attributedTitle = NSAttributedString(string: "Update data")
self.refresh.addTarget(self, action: #selector(updateFunction), for: .valueChanged)
if #available(iOS 10.0, *) {
self.tableView.refreshControl = self.refresh
} else {
self.tableView.addSubview(self.refresh)
}
}
@objc func updateFunction() {
//Your function to update the tableView
}
您需要实现UISearchBarDelegate 才能调用这些函数。
它也适用于 iOS 9
最好的问候