在表格视图中搜索栏

时间:2015-10-22 14:58:47

标签: ios swift uisearchbar

我有一个带有searchBar的tableView应用程序,显示一个包含杂货列表的数组。搜索完成后,用户点击杂货店并发送到第二张桌面视图。这工作正常,问题是:
在下一个(第二个)表视图中,有关杂货的信息,searchBar仍然存在,键盘也是如此。
如何关闭不应出现在第二个表视图中的键盘和searchBar。任何帮助都非常欢迎:)

//  DictionaryTableViewController.swift
    import UIKit

    class DictionaryTableViewController: UITableViewController, UISearchResultsUpdating
     {
        var searchController:UISearchController!
        var searchResults:[Dictionary] = []

        var dictionaries:[Dictionary] =[
         Dictionary (word: "grocery1", definition: "this is the grocery1 definition"),
         Dictionary (word: "grocery2", definition: "this is the grocery2 definition"),
         Dictionary (word: "grocery3", definition: "this is the grocery3 definition"),
         Dictionary (word: "grocery4", definition: "this is the grocery4 definition"),
        ]
        override func viewDidLoad() {
            super.viewDidLoad()

            searchController = UISearchController(searchResultsController: nil)
            tableView.tableHeaderView = searchController.searchBar
            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
        }    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }

        // MARK: - Table view data source

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            if searchController.active {
                return searchResults.count
            } else {
                return dictionaries.count
            }
        }

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cellIdentifier = "Cell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DictionaryTableViewCell

            let dictionary = (searchController.active) ? searchResults[indexPath.row]: dictionaries[indexPath.row]

            // Configure the cell...
            cell.wordLabel.text = dictionary.word
            return cell
        }

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "showDictionaryDetail" {
                if let indexPath = tableView.indexPathForSelectedRow {
                    let destinationController = segue.destinationViewController as! DictionaryDetailViewController
                    destinationController.dictionary = (searchController.active) ? searchResults[indexPath.row] : dictionaries[indexPath.row]
                }
            }
        }

            func updateSearchResultsForSearchController(searchController:
                UISearchController) {
                    if let searchText = searchController.searchBar.text {
                        filterContentForSearchText(searchText)
                        tableView.reloadData()
                    }
            }

            func filterContentForSearchText(searchText: String) {
                searchResults = dictionaries.filter({ (dictionary:Dictionary) -> Bool in
                    let wordMatch = dictionary.word.rangeOfString(searchText, options:
                        NSStringCompareOptions.CaseInsensitiveSearch)
                    return wordMatch != nil
                })}}

1 个答案:

答案 0 :(得分:2)

当您使用searchController时,您可以在离开屏幕时停用它。只需插入self.searchController.active = false

即可

您可以在viewWillDisappearprepareForSegue执行此操作。它看起来像这样

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDictionaryDetail" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let destinationController = segue.destinationViewController as! DictionaryDetailViewController
            destinationController.dictionary = (searchController.active) ? searchResults[indexPath.row] : dictionaries[indexPath.row]
            self.searchController.active = false
        }
    }
}