在uisearchcontroller中的三个字母后开始搜索

时间:2016-01-29 22:51:11

标签: ios swift uisearchdisplaycontroller

我希望uisearchcontroller在搜索栏中输入至少三个字符后开始搜索。那么,我该怎么做呢?

    func configureSearchController() {
    // Initialize and perform a minimum configuration to the search controller.
    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()

    let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as! UITextField
    textFieldInsideSearchBar.font = UIFont(name: "Bauhaus", size: 19)


   searchController.searchBar.setImage(UIImage(named: "searchikon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal);

    // Place the search bar view to the tableview headerview.
    TableView.tableHeaderView = searchController.searchBar

2 个答案:

答案 0 :(得分:1)

您需要的只是为UISearchController添加单个必需的方法。

func updateSearchResultsForSearchController(searchController: UISearchController)     {   
    if searchController.searchBar.text?.characters.count > 2 {
        // Filter your search results here
    }

}

答案 1 :(得分:0)

您需要检查检查文本字段中更改的事件中字符的长度:

nameOfString.characters.count