0

I add a search bar by adding subview into a UIView. When I tap the search bar, cancel button shows up, however the keyboard disappear immediately. I have to tap the search bar again so that I can input some text for searching. Any thoughts?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
henry
  • 83
  • 1
  • 13
  • Possible duplicate of [Close iOS Keyboard by touching anywhere using Swift](http://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift) – MRustamzade Sep 30 '16 at 08:19
  • its totally different question... Maybe you get me wrong for the question. i am not asking to close the keyboard by touching anywhere... – henry Sep 30 '16 at 08:20
  • keyboar must popup automatically. if you using simulator then go to: `hardware -> keyboard -> Toggle software keyboard` – MRustamzade Sep 30 '16 at 08:25
  • it popups and disappear immediately at the first time i press it. – henry Sep 30 '16 at 08:29
  • when you put something with keyboard it disappear. try it on phone. when it happens, please put some code to see what is wrong. – MRustamzade Sep 30 '16 at 08:32
  • Check where is the search string allocated and initialized. Probably, you would not have initialized the string yet during the first call! – sushma Sep 30 '16 at 08:40

1 Answers1

0

Use the following code:

import UIKit

class ViewController: UIViewController,UISearchDisplayDelegate, UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var headingLabel: UILabel!
    @IBOutlet weak var countriesTableView: UITableView!
    @IBOutlet weak var countrySerachBar: UISearchBar!

    var marrCountryList = [String]()
    var marrFilteredCountryList = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.countriesTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        marrCountryList = ["USA", "Bahamas", "Brazil", "Canada", "Republic of China", "Cuba", "Egypt", "Fiji", "France", "Germany", "Iceland", "India", "Indonesia", "Jamaica", "Kenya", "Madagascar", "Mexico", "Nepal", "Oman", "Pakistan", "Poland", "Singapore", "Somalia", "Switzerland", "Turkey", "UAE", "Vatican City"]
        self.countriesTableView.reloadData()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

         self.countrySerachBar.becomeFirstResponder()

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return self.marrFilteredCountryList.count
        } else {
            return self.marrCountryList.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cellCountry = self.countriesTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        var countryName : String!
        if tableView == self.searchDisplayController!.searchResultsTableView {
            countryName = marrFilteredCountryList[(indexPath as NSIndexPath).row]
        } else {
            countryName = marrCountryList[(indexPath as NSIndexPath).row]
        }
        cellCountry.textLabel?.text = countryName
        return cellCountry
    }

    func filterTableViewForEnterText(_ searchText: String) {
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
        let array = (self.marrCountryList as NSArray).filtered(using: searchPredicate)
        self.marrFilteredCountryList = array as! [String]
        self.countriesTableView.reloadData()
    }


    func searchDisplayController(_ controller: UISearchDisplayController, shouldReloadTableForSearch searchString: String?) -> Bool {
        self.filterTableViewForEnterText(searchString!)
        return true
    }


    func searchDisplayController(_ controller: UISearchDisplayController,
                                 shouldReloadTableForSearchScope searchOption: Int) -> Bool {
        self.filterTableViewForEnterText(self.searchDisplayController!.searchBar.text!)
        return true
    }

}

Storyboard screenshot:

enter image description here

Output:

enter image description here

Please check my GitHub link to test sample project:

https://github.com/k-sathireddy/SearchDisplayControllerSample

KSR
  • 1,699
  • 15
  • 22