我是Swift的新手。我面临的问题在搜索栏中。如果我输入文字,说明正在搜索。但是,一旦我在搜索栏外单击,文本就会消失,并带有过滤后的表格视图。我想保留文本,以便即使用户输入了错误的内容,他也应该理解搜索文本。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
var temp : String?
temp = UserDefaults.standard.value(forKey: "tagsareselected") as? String
if searchBar.text == nil || searchBar.text == ""
{
print("is searching is not working")
issearching = false
view.endEditing(true)
}
else if (temp == "0")
{
issearching = true
print("Searching is working")
filtercards = card.filter({ (card : Cards) -> Bool in
let didmatch = ((card.name.lowercased().hasPrefix(searchBar.text!.lowercased())) || (card.company.lowercased().hasPrefix(searchBar.text!.lowercased())) || (card.email.lowercased().hasPrefix(searchBar.text!.lowercased())) || (card.city.lowercased().hasPrefix(searchBar.text!.lowercased())) || (card.designation.lowercased().hasPrefix(searchBar.text!.lowercased())) || (card.phone.lowercased().hasPrefix(searchBar.text!.lowercased())))
return didmatch
})
}
cardsTable.reloadData()
}
即使用户使用过滤器table view
单击外部,如何保留文本?