我通过将子视图添加到UIView中来添加搜索栏。当我点击搜索栏时,取消按钮显示,但键盘立即消失。我必须再次点击搜索栏,以便我可以输入一些文本进行搜索。 有什么想法吗?
答案 0 :(得分:0)
使用以下代码:
import UIKit
class ViewController: UIViewController,UISearchDisplayDelegate, UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var headingLabel: UILabel!
@IBOutlet weak var countriesTableView: UITableView!
@IBOutlet weak var countrySerachBar: UISearchBar!
var marrCountryList = [String]()
var marrFilteredCountryList = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.countriesTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
marrCountryList = ["USA", "Bahamas", "Brazil", "Canada", "Republic of China", "Cuba", "Egypt", "Fiji", "France", "Germany", "Iceland", "India", "Indonesia", "Jamaica", "Kenya", "Madagascar", "Mexico", "Nepal", "Oman", "Pakistan", "Poland", "Singapore", "Somalia", "Switzerland", "Turkey", "UAE", "Vatican City"]
self.countriesTableView.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.countrySerachBar.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.marrFilteredCountryList.count
} else {
return self.marrCountryList.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cellCountry = self.countriesTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var countryName : String!
if tableView == self.searchDisplayController!.searchResultsTableView {
countryName = marrFilteredCountryList[(indexPath as NSIndexPath).row]
} else {
countryName = marrCountryList[(indexPath as NSIndexPath).row]
}
cellCountry.textLabel?.text = countryName
return cellCountry
}
func filterTableViewForEnterText(_ searchText: String) {
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
let array = (self.marrCountryList as NSArray).filtered(using: searchPredicate)
self.marrFilteredCountryList = array as! [String]
self.countriesTableView.reloadData()
}
func searchDisplayController(_ controller: UISearchDisplayController, shouldReloadTableForSearch searchString: String?) -> Bool {
self.filterTableViewForEnterText(searchString!)
return true
}
func searchDisplayController(_ controller: UISearchDisplayController,
shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterTableViewForEnterText(self.searchDisplayController!.searchBar.text!)
return true
}
}
请检查我的GitHub链接以测试示例项目:
https://github.com/k-sathireddy/SearchDisplayControllerSample