简体   繁体   English

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

[英]How to change background of a cell when the name contains the search bar text?

I am trying to change the color of a cell if the name of the cell matches with part of the text in the search bar.如果单元格的名称与搜索栏中的部分文本匹配,我正在尝试更改单元格的颜色。 Almost as the cell is being highlighted.几乎在单元格被突出显示时。 I am using a search bar and a table view for this.我为此使用了搜索栏和表格视图。

I have tried this code with the help of the comments but I still can't figure it out:我在评论的帮助下尝试了这段代码,但我仍然无法弄清楚:

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()

    }

}

The goal was to try and use the .filter to separate the cells that don't contain the search bar text with what does.目标是尝试使用 .filter 将不包含搜索栏文本的单元格与包含的内容分开。 Then change the color of the cell if it does contain that text.如果单元格确实包含该文本,则更改单元格的颜色。

This is something that you can do.这是你可以做的事情。

you can filter your search array whenever there is a change in search text.只要搜索文本发生变化,您就可以过滤搜索数组。

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()
 }

and in willDisplayCell you do this并在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.

}

And to reset it, you can set your searchElements array to empty.要重置它,您可以将 searchElements 数组设置为空。

//NOTE: The willDisplay Method need not go inside the textDidChange method //注意: willDisplay方法不需要进入textDidChange方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM