我已经使用UITextField创建了自定义搜索栏,并在其下方显示了UITableView,它将显示结果。如何显示消息,即在搜索栏中键入的字符串不匹配时找不到记录
我的搜索控制器文件代码:
class SearchPageController: UIViewController {
@IBOutlet weak var searchTxtBar: UITextField!
@IBOutlet weak var searchTblView: UITableView!
@IBOutlet weak var helpMessageView: UIView!
var CatStoreData:[String] = Array()
var filteredData:[String] = Array()
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
self.helpMessageView.isHidden = true
// Do any additional setup after loading the view.
searchTxtBar.delegate = self
//append data to catstoredata array
CatStoreData.append("costco.com.au")
CatStoreData.append("au.kogan.com")
CatStoreData.append("rebelsport.com.au")
CatStoreData.append("ebgames.com.au")
CatStoreData.append("computeralliance.com.au")
CatStoreData.append("harveynorman-com-au")
CatStoreData.append("binglee.com.au")
CatStoreData.append("iwantthatflight.com.au")
CatStoreData.append("mobileciti.com.au")
CatStoreData.append("Electronics and Gadgets")
CatStoreData.append("Baby Care")
CatStoreData.append("Computers")
CatStoreData.append("Computers and Laptops")
CatStoreData.append("Entertainment")
CatStoreData.append("Eyewear")
CatStoreData.append("Flights")
CatStoreData.append("Home & Kitchen")
for data in CatStoreData {
filteredData.append(data)
}
searchTxtBar.addTarget(self, action: #selector(searchRecords(_ :)), for: .editingChanged)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
@objc func searchRecords(_ textfield: UITextField) {
self.CatStoreData.removeAll()
if searchTxtBar.text?.count != 0 {
for data in filteredData {
if let dataToSearch = self.searchTxtBar.text {
let range = data.lowercased().range(of: dataToSearch, options: .caseInsensitive, range: nil, locale: nil)
if range != nil {
CatStoreData.append(data)
}
}
}
}else {
for data in filteredData {
CatStoreData.append(data)
}
}
searchTblView.reloadData()
}
//MARK: IBActions
@IBAction func toHomeScreen(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
@IBAction func clearSearchData(_ sender: UIButton) {
self.CatStoreData.removeAll()
self.searchTxtBar.text = ""
for data in filteredData {
CatStoreData.append(data)
}
searchTblView.reloadData()
}
}
Tableview委托和数据源方法
extension SearchPageController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CatStoreData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "catstoredata")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "catstoredata")
}
cell?.selectionStyle = .none
cell?.textLabel?.text = CatStoreData[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.popViewController(animated: true)
}
}
文本字段委托方法
extension SearchPageController: UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.searchTxtBar.resignFirstResponder()
return false
}
}