简体   繁体   English

UISearchController问题快速iOS

[英]UISearchController issue swift ios

I've a tableview with a search bar on it. 我有一个带有搜索栏的表格视图。 I can search and it displays the correct results. 我可以搜索,并且显示正确的结果。 When, I click on cancel, my tableview got all the data on it which is good. 当我单击“取消”时,我的表视图上显示了所有数据,这很好。 What I'm trying to achieve is, while searching, if I hit the return button on the keyboard, and when my search bar is empty, I'd like to have my initial data displayed. 我想要实现的是,在搜索时,如果我按下了键盘上的返回按钮,并且当搜索栏为空时,我想显示我的初始数据。 Here's my code for searching. 这是我的搜索代码。 Basically it's our the contacts address book works on iPhone. 基本上,这是我们在iPhone上使用的通讯录。

Any help will be welcomed. 任何帮助都将受到欢迎。

 func setupSearchBar() {
        searchController = self.addSearchBar()
        searchController.searchResultsUpdater = self
        searchController.delegate = self
        searchController.searchBar.delegate = self
    }
func updateSearchResultsForSearchController(searchController: UISearchController) {
        if count(searchController.searchBar.text) > 0 {
            tableData = MyCoreData().search(searchController.searchBar.text)
            self.tableView.reloadData()
        }
    }

func searchBarCancelButton(searchBar: UISearchBar) {
    searchBar.text = ""
    self.resignFirstResponder()
    self.view.endEditing(true)
    searchBar.resignFirstResponder()
    reloadTableDataFromDB()
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
}

Your updatesearch function is missing a else part to return the default loaded data. 您的updatesearch函数缺少返回默认加载数据的else部分。 Do this: 做这个:

 func setupSearchBar() {
        searchController = self.addSearchBar()
        searchController.searchResultsUpdater = self
        searchController.delegate = self
        searchController.searchBar.delegate = self
    }
func updateSearchResultsForSearchController(searchController: UISearchController) {
        if count(searchController.searchBar.text) > 0 {
            tableData = MyCoreData().search(searchController.searchBar.text)
            self.tableView.reloadData()
        } else {
        return self.reloadDataFromDB()
}
    }

func searchBarCancelButton(searchBar: UISearchBar) {
    searchBar.text = ""
    self.resignFirstResponder()
    self.view.endEditing(true)
    searchBar.resignFirstResponder()
    reloadTableDataFromDB()
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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