2017-08-08 69 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 接受答案,如果它帮助:) –