Uitableview Swift-搜索时禁用刷新控制

Uitableview Swift-搜索时禁用刷新控制,uitableview,swift,uirefreshcontrol,Uitableview,Swift,Uirefreshcontrol,在搜索过程中,我想禁用pull to refresh机制。所以我禁用了刷新控件并将其删除。但是当下拉刷新时,会调用beginRefresh方法,并且单元格会像刷新一样持续关闭2秒钟 func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool { resultSearchController.searchBar.selectedScopeButtonIndex = 0 refreshControl!.ena

在搜索过程中,我想禁用pull to refresh机制。所以我禁用了刷新控件并将其删除。但是当下拉刷新时,会调用beginRefresh方法,并且单元格会像刷新一样持续关闭2秒钟

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
    resultSearchController.searchBar.selectedScopeButtonIndex = 0
    refreshControl!.enabled = false
    refreshControl?.removeFromSuperview()
    return true
}

如果要在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
}

搜索时,检查refreshControl是否具有superView,并在搜索结束后从superView中删除。再次添加它,条件如下所示:

  if self.refreshControl.isDescendant(of: self.tblView) {
      self.refreshControl.removeFromSuperview() 
  }

尝试使用此代码,它对我有效:

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

它也适用于iOS9

致意