当名称包含搜索栏文本时,如何更改单元格的背景?



如果单元格的名称与搜索栏中的部分文本匹配,我正在尝试更改单元格的颜色。几乎在单元格突出显示时。我为此使用搜索栏和表格视图。

我已经在注释的帮助下尝试了这段代码,但我仍然无法弄清楚:

let allElements = ["Hydrogen", "Helium", ...]
var searchElement = [String]()
var searching = false
var myIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row selected : (indexPath.row)")
if indexPath.row == 0 {
let hydrogenSearchSegue = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as! hydrogenViewController
self.navigationController?.pushViewController(hydrogenSearchSegue, animated:true)


}
}
}

extension searchViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching == true {
return searchElement.count
} else {
return allElements.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if searching == true {
cell?.textLabel?.text = searchElement[indexPath.row]
} else {
cell?.textLabel?.text = allElements[indexPath.row]
}
return cell!
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let string = allElements[indexPath.row]
if searchElement.contains(string) {
cell.backgroundColor = UIColor.red
} else {
cell.backgroundColor = UIColor.white
}
}
}
extension searchViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchElement = allElements.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
searching = true
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
_ = navigationController?.popViewController(animated: true)
searchBar.text = ""
tableView.reloadData()
}
}

目标是尝试使用 .filter 将不包含搜索栏文本的单元格与包含搜索栏文本的单元格分开。然后更改单元格的颜色(如果它确实包含该文本(。

这是你可以做的事情。

只要搜索文本发生更改,您就可以筛选搜索数组。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
//here searchElement is your searched array. and allElements is your main array
searchElement = allElements.filter({$0.lowercased().prefix(searchText.count) 
// or your filter logic whatever it is. 
//and then you refresh your tableview here
myTableView.reloadData()
}

willDisplayCell你这样做

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let string = allElements[indexPath.row]
//if your search array contains the current cell, then you can show the cell as highlighted.
if searchElement.contains(string) {
cell.backgroundColor = UIColor.red
}else {
cell.backgroundColor = UIColor.white
}
//But make sure to empty your search array in case you are not searching.
}

要重置它,您可以将搜索元素数组设置为空。

注意:willDisplay方法不需要进入textDidChange方法内部

最新更新