简体   繁体   中英

Search Bar in a table view

I have a tableView application with a searchBar showing a array with a list of groceries. After a search is made the user click on the grocery and send to a second tableview. That is working fine, the problem is:
In the next (second) table view with the information about the grocery the searchBar still there and the keyboard as well.
How can I dismiss the keyboard and the searchBar that should not appear in the second table view. Any help is more than welcome :)

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

When you are using a searchController, you can just deactivate it when you are leaving the screen. Just insert self.searchController.active = false

You can do it at viewWillDisappear or prepareForSegue . It would look something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM