2017-08-08 1 views
0

Configurer un chercheur dans la barre de navigation dans un UITable. Tout fonctionne bien, acceptez le fait que lorsque j'ai recherché et trouvé les cellules nécessaires et que je veux cliquer sur cette cellule, le terme de recherche est rapidement retiré de la barre de recherche. Le résultat est que je récupère la "vieille" table. Je ne peux pas utiliser les résultats de la recherche.Swift 3: appuyez en dehors de la barre de recherche, la recherche est automatique supprimée

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

Que voulez-vous faire lorsque vous cliquez sur un élément de la cellule? –

+0

Si je clique sur l'élément de cellule et obtiens une extension de trois lignes, les lignes pointent vers un Viewcontroller suivant. –

+0

Le problème est dans le fait, que les résultats de la recherche sont supprimés lorsque je veux appuyer sur les résultats (infacte everey où j'appuie). –

Répondre

0

Je vous suggère de mettre des points d'arrêt sur votre searchBarTextDidBeginEditing, searchBarCancelButtonClicked, searchBarTextDidEndEditing, searchBarSearchButtonClicked et updateSearchResults. Lorsque vous sélectionnez la cellule et que le résultat de la recherche disparaît, voir où s'arrête le point d'arrêt. Il semble qu'il pourrait y avoir des problèmes de logique avec shouldShowSearchResults et searchWasCancelled. Parce que de ce que vous dites est correct, alors votre searchController.searchBar.resignFirstResponder() est déclenché ce qui annule la recherche. Mettre en place des points d'arrêt et débogueur vous obtiendrez le coupable.

+0

Merci! Malheureusement, je ne suis pas professionnel avec Swift, quand j'active les points d'arrêt, j'ai quelques surprises. En ce moment je n'ai pas le temps de les réparer. Mais j'ai trouvé le problème, dimsBackgroundDuringPresentation sur false ..... Je n'ai aucune idée de ce qu'il fait et si c'est nécessaire ....? –

+0

Vous pouvez le définir comme faux ou l'omettre tous ensemble car c'est facultatif. Voici plus d'informations à ce sujet https://developer.apple.com/documentation/uikit/uisearchcontroller/1618660-dimsbackgroundduringpresentation?language=objc Accepter la réponse si cela a aidé :) –