Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Disable refreshControl while searching

During the search I want to disable the pull to refresh mechanism. So I disabled the refresh control and removed it. But when pull down to refresh the beginRefresh method is called and the cells keep being down for 2 seconds like there is a refresh.

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
    resultSearchController.searchBar.selectedScopeButtonIndex = 0
    refreshControl!.enabled = false
    refreshControl?.removeFromSuperview()
    return true
}
like image 958
Sven Mäurer Avatar asked Jun 01 '15 19:06

Sven Mäurer


2 Answers

Use these two functions if you want to create or delete refresh controls in the top of 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
}
like image 129
Muratov Bahtiyor Avatar answered Sep 28 '22 03:09

Muratov Bahtiyor


When search then check if refreshControl has superView and Remove from superView After search End Add it again, the condition will look like :

  if self.refreshControl.isDescendant(of: self.tblView) {
      self.refreshControl.removeFromSuperview() 
  }
like image 33
Rajpal Thakur Avatar answered Sep 28 '22 02:09

Rajpal Thakur