Swift触摸返回键时如何更新搜索结果

时间:2015-02-06 03:38:51

标签: swift uisearchcontroller

我Swift,我使用UISearcheControllerUISearcheResultUpdating

创建了搜索视图

我用func updateSearchResultsForSearchController

更新结果
if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {  
        let searchBarText = self.searchController!.searchBar.text
        var arrResult = DFManager.GetResult(searchBarText)
        self.results?.addObjectsFromArray(arrResult)            
        // Reload a table with results.
        self.searchResultsController?.tableView.reloadData()
    }

但是当我用char键入char时,结果总是会更新,它会使这个应用程序变慢。我希望这个结果只在类型返回键时更新?

1 个答案:

答案 0 :(得分:0)

试试这个:

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        //do something
        searchBar.resignFirstResponder() //hide keyboard

    }

不要忘记添加searchBar委托:

class SomeViewController: UIViewController, UISearchBarDelegate {

   @IBOutlet weak var searchBar: UISearchBar!

   override func viewDidLoad() {
        searchBar.delegate = self
   }

}