2017-08-08 45 views
0

在UITable的導航條中配置搜索者。一切工作正常,接受這樣一個事實,即當我搜索並找到需要的單元格並且想要單擊該單元格時,搜索詞將很快從搜索欄中刪除。結果是我找回了「舊」表。我無法使用搜索結果。Swift 3:按下外部搜索條,搜索被自動刪除

Class property: UITableViewController, ExpandableHeaderViewDelegate, UISearchBarDelegate, UISearchResultsUpdating { 

    var filteredArray = [Mijnproducten]() 
    var shouldShowSearchResults = false 

override func viewDidLoad() { 
     super.viewDidLoad() 


     configureSearchController() 

     } 

    func configureSearchController() { 
     // Initialize and perform a minimum configuration to the search controller. 
     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = true 
     searchController.searchBar.placeholder = "Search here..." 
     searchController.searchBar.sizeToFit() 
     searchController.hidesNavigationBarDuringPresentation = false 

     // Place the search bar view to the tableview headerview. 
     self.navigationItem.titleView = searchController.searchBar 
    } 


    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 
     shouldShowSearchResults = true 
     searchWasCancelled = false 
     self.tableView.reloadData() 
    } 

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
     shouldShowSearchResults = false 
     searchWasCancelled = true 
     self.tableView.reloadData() 

    } 

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { 
     var searchTerm = "" 

     if searchWasCancelled { 
      searchController.searchBar.text = searchTerm 
     } 
     else { 
      searchTerm = searchController.searchBar.text! 
     } 
    } 

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
     let text = searchController.searchBar.text 
     searchController.searchBar.text = text 
     if !shouldShowSearchResults { 
      shouldShowSearchResults = false 
      self.tableView.reloadData() 
     } 

     searchController.searchBar.resignFirstResponder() 
    } 

    func updateSearchResults(for searchController: UISearchController) { 

     let searchtext = searchController.searchBar.text 
     print(searchtext) 

     filteredArray = mijnproducten01.filter{ product in 
      return product.productname.lowercased().contains((searchtext?.lowercased())!) 
     } 
     print(filteredArray) 

     if searchtext?.isEmpty == false 
     { shouldShowSearchResults = true } 
     else 
     { shouldShowSearchResults = false } 

     // Reload the tableview. 
     self.tableView.reloadData() 
     } 

    } 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let indexPath = tableView.indexPathForSelectedRow 
     let currentCell = tableView.cellForRow(at: indexPath!) as! RowTableViewCell 
     let RowName = currentCell.ProductLine!.text 
     let temp = currentCell.UUID!.text 
     print("jaaaa", temp) 
     TAGID = Int(temp!)! 


     if RowName == "History" 
     { 
      tableView.deselectRow(at: indexPath!, animated: true) 
      self.performSegue(withIdentifier: "PropertyToHistory", sender: self) 
     } 
     else if RowName == "Description" 
     { 
      tableView.deselectRow(at: indexPath!, animated: true) 
      self.performSegue(withIdentifier: "PropertyToDescription", sender: self) 
     } 
     else if RowName == "Transfer" 
     { 
      tableView.deselectRow(at: indexPath!, animated: true) 
      self.performSegue(withIdentifier: "PropertyToTransfer", sender: self) 
     } 
    } 
+0

當單元格項被點擊時,你想做什麼? –

+0

如果我單擊單元格Item並獲得三個獲取行的可擴展,則這些行指向下一個Viewcontroller。 –

+0

問題在於,當我想按結果時,搜索結果被刪除(實際上我按下的是everey)。 –

回答

0

我建議你把斷點您searchBarTextDidBeginEditingsearchBarCancelButtonClickedsearchBarTextDidEndEditingsearchBarSearchButtonClickedupdateSearchResults。 當您選擇單元格並且搜索結果消失時,請查看斷點停止的位置。

看起來可能存在邏輯問題shouldShowSearchResultssearchWasCancelled

因爲從你說的是正確的,那麼你的searchController.searchBar.resignFirstResponder()就會被觸發,從而取消搜索。放置斷點和調試器會讓你成爲罪魁禍首。

+0

謝謝!不幸的是,我並不是那種專業性很強的人,當我啓用斷點時,我會遇到一些驚喜。在這一刻,我沒有時間來解決這些問題。但我發現這個問題,dimsBackgroundDuringPresentation上的錯誤.....我不知道它做了什麼,如果需要....? –

+0

您可以將它設置爲false或將其忽略,因爲它是可選的。這裏是關於它的更多信息https://developer.apple.com/documentation/uikit/uisearchcontroller/1618660-dimsbackgroundduringpresentation?language=objc 接受答案,如果它幫助:) –