iOS UISearchController TableView

标签 ios iphone uitableview uitabbarcontroller uisearchcontroller

我有一个 UISearchController,在 NavigationBarTitleView 中有一个 UISearchBar。当您开始在 SearchBar 中键入内容时,会出现一个 ResultsTableView。键入时 SearchBar 旁边会出现一个取消按钮,它会正确关闭 TableView。到目前为止,一切都很好。

场景的主 viewController 是一个 TableViewController,它连接到 TabBarController 中的 NavigationController

现在,一切正常,除了当显示 ResultsTableView 并且您离开该屏幕(通过 tabBar)时,当您返回时屏幕变黑

我想我需要在导航离开时关闭 resultsTableView 以防止这种情况(就像我在触发结果的 segue 时所做的那样)但我无法让它工作。

这是一些代码:

class PCFeedTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating, UISearchControllerDelegate {

    let searchController = UISearchController(searchResultsController: PCSearchTableViewController())

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        let resultsTable = searchController.searchResultsController as! PCSearchTableViewController
        resultsTable.tableView.frame.origin = CGPoint(x: 0, y: 64)
        resultsTable.tableView.frame.size.height = self.tableView.frame.height - 113
        resultsTable.tableView.tableHeaderView = nil
        resultsTable.tableView.contentInset = UIEdgeInsetsZero
        resultsTable.tableView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        resultsTable.parentController = self
        resultsTable.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "searchResultCell")
    }

...

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.scrollsToTop = true

        self.searchController.searchResultsUpdater = self
        self.searchController.searchBar.delegate = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = false
        self.searchController.searchBar.sizeToFit()
        self.searchController.searchBar.placeholder = "Search for Movies or TV Shows"
        self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        self.searchController.searchBar.barStyle = UIBarStyle.Black
        UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.lightTextColor()
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()

        self.navigationItem.titleView = searchController.searchBar
    }


...

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let destinationViewController = segue.destinationViewController as! PCMainDetailsTableViewController
        self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
        self.searchController.searchBar.text = nil
        destinationViewController.title = searchResult.title
        destinationViewController.movie = searchResult

    }
}

最佳答案

尝试将关闭搜索 Controller 的代码放在 viewDidDisappear: 中,如下所示:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)        
    self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
    self.searchController.searchBar.text = nil
}

关于iOS UISearchController TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34199411/

相关文章:

ios - 如何将数据放在 UITableViewCell 的右侧

iphone - 为什么执行选择器:withObject: methods can only take id?

iphone 的 Python 服务器端推送通知

ios - block 对象会导致保留周期吗?

ios - xcode 8.2 中显示 'RCTLogLevel' 错误的重新定义

iphone - iAD 被 UITableView 索引遮挡

iphone - 使用 scrollToRowAtIndexPath 滚动到最后一个 UITableViewCell :atScrollPosition:animated using NSFetchedResultsControllerDelegate methods

ios - 将选定的行复制到一个数组,该数组填充 Xcode 中的另一个 UITableView

ios - UITableView 不加载数据-带有 Xcode 7.2 的 Swift 2

ios - Apple 方式的 UITableView 事件指示器