Swift 3 : press outside searchBar, 搜索被自动删除

标签 swift uinavigationcontroller uisearchbar

在 UITable 的导航栏中配置了一个搜索器。一切工作正常,接受这样一个事实:当我搜索并找到所需的单元格并且我想单击该单元格时,搜索词很快就会从搜索栏中删除。结果是我拿回了“旧”表。我无法使用搜索结果。

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)
        }
     }

最佳答案

我建议您在 searchBarTextDidBeginEditingsearchBarCancelButtonClickedsearchBarTextDidEndEditingsearchBarSearchButtonClicked 上放置断点>更新搜索结果。 当您选择单元格并且搜索结果消失时,请查看断点停止的位置。

似乎 shouldShowSearchResultssearchWasCancelled 可能存在逻辑问题。

因为您所说的是正确的,所以您的 searchController.searchBar.resignFirstResponder() 会被触发,从而取消搜索。设置断点和调试器会让你找到罪魁祸首。

关于Swift 3 : press outside searchBar, 搜索被自动删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45571264/

相关文章:

ios - 使用 UIBarButtonItem 将 UISearchBar 添加到 UINavigationBar

objective-c - 向导航栏添加两个右栏按钮项

ios - 如何使用 RxSwift 和 RxSwiftDataSources 将 TableView 与代表不同数据类型的多个部分绑定(bind)?

SwiftUI 核心数据,分组列表获取结果

iOS:如何在现有的 UINavigationController 中打开另一个 UINavigationController?

ios - UISearchController 在结构中搜索结构数组

ios - UISearchBar 宽度动画和 AutoLayout

swift - 如何隐藏搜索栏并使其在按下按钮时显示

ios - Swift 函数在应用程序中有效,但在 override func viewDidLoad() 中无效

ios - Swift 如何在单击按钮时更新 AVPlayer 中的视频 url?