Using a search bar

1. Let view controller class inherit: UISearchBarDelegate
2. Add a search bar to the storyboard, constrain it and add an outlet to it.
3. In ViewDidLoad:

theSearchBar.delegate = self as UISearchBarDelegate
        theSearchBar.returnKeyType = UIReturnKeyType.done
     

4. Class variable: var isSearching = false
5. Implement the following method:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == nil || searchBar.text == ""
        {
            isSearching = false
            view.endEditing(true)
//function to get the data and reload the table
            getData()
            ViewMyTasks.reloadData()
        }
        else
        {
            isSearching = true
            getDataWithSearch(searchText: searchText)
            ViewMyTasks.reloadData()
        }



Comments

Popular posts from this blog

Setting up a playground

Go to another page