extension TodoListViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let request: NSFetchRequest<Item> = Item.fetchRequest()
let predicate = NSPredicate(format: "title CONTAINS %@", searchBar.text!)
request.predicate = predicate
let sordDescriptr = NSSortDescriptor(key: "title", ascending: true)
request.sortDescriptors = [sordDescriptr]
do {
itemArray = try context.fetch(request)
} catch {
print("Error fetching data from context \\(error)")
}
tableView.reloadData()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text?.count == 0 {
loadItems()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
// uisearchbar first letter lower capitalize???
searchBar.searchTextField.autocapitalizationType = .none
First Letter Capitalize | https://stackoverflow.com/questions/34529909/uitextfield-and-uisearch-bar-disable-capital-letters-button-swift
ResignFirstResponder | https://developer.apple.com/documentation/uikit/uiresponder/1621097-resignfirstresponder