Swift - 在搜索

时间:2015-06-01 19:22:15

标签: uitableview swift uirefreshcontrol

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

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

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

如果要在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 :(得分:0)

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

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

最诚挚的问候

相关问题