快速搜索导航项未更新

时间:2018-09-07 04:37:34

标签: ios swift

我创建了一个搜索导航栏,以使用户可以轻松地在长列表中选择他们想要的条目。假设用户输入“ a”,则只会显示以“ a”开头的用户条目。但是,当我在搜索栏上键入任何内容时,没有任何显示。我在viewDidLoad中输入了此代码。

const char *

下面是典型的tableview函数。

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    navigationItem.searchController = UISearchController(searchResultsController: nil)
    navigationItem.hidesSearchBarWhenScrolling = false

}

此外,我似乎找不到在搜索栏中更改占位符的方法。我将不胜感激。预先谢谢你!

3 个答案:

答案 0 :(得分:2)

尝试此代码。您需要添加UISearchBar委托才能工作。

您需要更正以下内容。

1。。您在numberOfRowsInSection中返回不正确的静态22行。首先,您需要确定用户是否在搜索任何内容,并以此为基础来返回数据。

2。。您可以在identifiers中定义cellForRowAt数组。与numberOfRowsInSection相同,您需要通过检查用户是否搜索任何内容来返回数据。

您需要执行以下操作

首先在全局定义2个数组第一个是您的标识符数组第二个是您的搜索者数据数组

var identifiers = [String]()
var arrSearcherData = [String]()
var isSearching: Bool = false    // Add this to identify wether user is searching data or not

viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    identifiers = ["Adana","Ankara","Antalya","Balikesir","Bursa","Denizli","Diyarbakir","Eskisehir","Gaziantep","Hatay","Icel","Istanbul","Izmir","Kayseri","Kocaeli","Konya","Manisa","Sakarya","Samsun","Sivas","Trabzon","Sanliurfa"]

    navigationItem.searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController?.searchBar.delegate             = self
    navigationItem.searchController?.searchBar.showsCancelButton    = true
    navigationItem.hidesSearchBarWhenScrolling                      = false
    self.definesPresentationContext                                 = true
}

您的TableView委托和数据源

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isSearching {
            return arrSearcherData.count
        }
        return identifiers.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")

        if isSearching {
            cell.textLabel?.text = arrSearcherData[indexPath.row]
        }
        else {
            cell.textLabel?.text = identifiers[indexPath.row]
        }

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

    }
}

您的SearcherBar代表

//MARK:- UISearchBar Delegate
extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        self.tblvw.reloadData()

        if searchText.count == 0 {
            isSearching = false
        }
        else {
            isSearching = true
            arrSearcherData = identifiers.filter { $0.contains(searchText) }
        }
        self.tblvw.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()
        isSearching = false
        self.tblvw.reloadData()
    }
}

答案 1 :(得分:0)

根据上述信息,尚不清楚代码中有什么问题。但是请检查代码中是否调用了“ searchBarShouldBeginEditing”委托。

在此委托中,您需要编写代码以触发搜索条目。

答案 2 :(得分:0)

听起来您只是在情节提要中添加了搜索栏,并希望它能正常工作。在搜索栏中键入内容时,您需要告诉它该怎么做。为此,您需要适当的委托和搜索实现。

查看这些链接以获取更多信息:

在班级顶部添加此内容,以获取班级可以在任何地方使用的内部全局变量:

var searchController : UISearchController! = UISearchController(searchResultsController: nil)

现在实施适当的协议:

extension YourTableViewController : UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        if let text : String = searchController.searchBar.text {
            // Process data here in some kind of for loop.
            self.tableView.reloadData()
        }
    }
}

有关其工作原理的更多信息:https://developer.apple.com/documentation/uikit/uisearchresultsupdating

将此函数放在您的班级或其他地方。只要确保您可以随意调用即可。随意使用此代码并对其进行修改。您可以在结构中使用此单个函数(如果是,请将其标记为“静态”),然后从任何地方调用它,以使搜索栏实现在整个应用程序中保持一致。

func setUpSearchController(for searchController: UISearchController, placeHolder: String, tableViewController: UITableViewController, searchControllerDelegate: UISearchControllerDelegate, searchUpdaterDelegate: UISearchResultsUpdating, searchBarDelegate: UISearchBarDelegate) {
    // SearchBar text
    let textFieldInsideUISearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField /* Do not edit this key. This is not where you put your own placeholder. Put your placeholder in the function call. */
    textFieldInsideUISearchBar?.font = Constants.MAIN_FONT /* Add your font here or add as a parameter */
    // SearchBar placeholder
    let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel /* Do not edit this key. This is not where you put your own placeholder. Put your placeholder in the function call. */
    textFieldInsideUISearchBarLabel?.font = UIFont.systemFont(ofSize: 15.0) /* Customize or add your font here or add as a parameter */
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.barStyle = .black
    searchController.searchBar.tintColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) /* Customize or add your own color as a parameter*/
    searchController.searchBar.delegate = searchBarDelegate
    searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.light
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = placeHolder
    searchController.definesPresentationContext = true
    searchController.searchResultsUpdater = searchUpdaterDelegate
    tableViewController.navigationItem.titleView = searchController.searchBar
    tableViewController.definesPresentationContext = true
}

要调用该函数,请按以下方式调用该函数(假设您将该函数放在类中。):

self.setUpSearchController(searchController: self.searchController, placeHolder: "PlaceHolder Text", tableViewController: self, searchControllerDelegate: self, searchUpdaterDelegate: self, searchBarDelegate: self)

如果看不到搜索控制器,请确保未隐藏导航栏,并确保您的表格视图已嵌入导航控制器中。

祝你好运!