[英]Why is the table view not resetting the results after the cancel button is clicked?
[英]Why is my searchBar not resetting to the initial state after the cancel button is clicked?
我有一個搜索欄與取消按鈕。 當我單擊取消按鈕時,它不會將searchBar重置為其初始狀態。
這是完整的源代碼。
var searchBar: UISearchBar = UISearchBar()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBarText = searchBar.text?.lowercased()
searchBarScope = searchBar.selectedScopeButtonIndex
showList()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = nil
searchBar.setShowsCancelButton(false, animated: true)
searchBar.endEditing(true)
}
public func setupSearchBarStyle() {
UISearchBar.appearance().searchBarStyle = .minimal
UISearchBar.appearance().backgroundColor = UIColor.white
UISearchBar.appearance().barTintColor = UIColor.white
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsScopeBar = true
if (navigationItem.title != nil) {
self.searchBar.scopeButtonTitles = [String(format: "gesamte %@", ci("project_s")), String(format: "in %@", navigationItem.title!)]
}
}
override func viewDidLayoutSubviews() {
self.searchBar.sizeToFit()
}
public override func viewDidLoad() {
self.navigationController?.setNavigationBarHidden(false, animated: false)
navigationItem.title = navigationItem.title ?? ci("plan_p")
guard let projectId = GlobalState.selectedProjectId, let byProject : Results<Structure> = self.by(projectId: projectId) else {
return
}
//search bar
tableView.rowHeight = 100.0
tableView.tableHeaderView = searchBar
self.searchBar.showsCancelButton = true
self.searchBar.sizeToFit()
self.definesPresentationContext = true
self.searchBar.delegate = self
tableView.allowsMultipleSelectionDuringEditing = true
用戶單擊取消按鈕后,如何將searchBar更改為其初始狀態? 現在,在用戶單擊取消后,它會將searchText更改為nil 。
在searchBarCancelButtonClicked
函數中:
searchBar.text = ""
searchBar.showsCancelButton = false
同樣在textDidChange
:
yourSearchBar.showsCancelButton = true
我認為是因為這段代碼:
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
它一定要是 :
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.setShowsCancelButton(false, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.