首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我第一次点击搜索栏时,键盘自动关闭

当我第一次点击搜索栏时,键盘自动关闭
EN

Stack Overflow用户
提问于 2016-09-30 16:17:14
回答 1查看 370关注 0票数 0

我通过在UIView中添加子视图来添加搜索栏。当我点击搜索栏时,取消按钮出现了,但是键盘立即消失了。我必须再次点击搜索栏,这样我才能输入一些文本进行搜索。有什么想法吗?

EN

Stack Overflow用户

发布于 2016-09-30 17:14:10

使用以下代码:

代码语言:javascript
运行
复制
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
    }

}

故事板屏幕截图:

输出:

请查看我的GitHub链接以测试示例项目:

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

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39786780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档