单击指定的单元格后,获取 Table 在搜索栏占位符中查看单元格文本值

Getting Table View Cell text Value in Searchbar Placeholder once a specified cell is clicked

我的 ViewController 包含一个 Table 视图。我从 JSON 获取数据并且已经能够在我的 Table 视图单元格 中显示这些值。还有 搜索栏 以及用于过滤目的。

我想将我的 Table 查看单元格文本 作为我的 搜索栏
占位符 单击特定单元格时。

我想在单击该特定单元格时将 Baramunda 作为我的 占位符文本

我的代码如下。

func searchBar(){

    let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50));
    searchBar.delegate = self
    searchBar.showsScopeBar = true
    searchBar.tintColor = UIColor.green
    self.view .addSubview(searchBar)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText == ""
    {
        tableView.alpha = 0.0
        searchBar.placeholder = "Search Location Here"
        parseData()
    }
    else {

        if searchBar.selectedScopeButtonIndex  == 0 {
            tableView.alpha = 1.0
            areaNameArr = areaNameArr.filter({ (anyobject) -> Bool in
                return (anyobject.lowercased.contains(searchText.lowercased()))
            })

        }
        else {
            print("Do Nothing")
        }
    }
    self.tableView.reloadData()
}


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

{

    return areaNameArr.count

}



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

{
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell" )
    cell?.backgroundColor = UIColor .black
    cell?.textLabel?.textColor = UIColor .white
    cell?.textLabel?.text = areaNameArr[indexPath.row] as? String
    return cell!
}

我现在没有xcode,但下面应该有用。

在 tableView 的 didSelectCell 委托方法中添加以下代码

var searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
if searchTextField!.responds(to: #selector(getter: UITextField.attributedPlaceholder)) {
    let attributeDict = [NSAttributedStringKey.foregroundColor: UIColor.white]
    searchTextField!.attributedPlaceholder = NSAttributedString(string: "Search", attributes: attributeDict)
}

您需要将 searchBar 变量声明为全局变量,以便在您的函数 searchBar() 外部访问。

class ViewController: UIViewController{
let searchBar = UISearchBar()
 override func viewDidLoad() {
 super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
   //here goes your code of viewDidLoad
}
//This is your function for creating search bar ,only first line is changed and rest is same
func searchBar(){
searchBar.frame(forAlignmentRect: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50))
searchBar.delegate = self
searchBar.showsScopeBar = true
searchBar.tintColor = UIColor.green
self.view .addSubview(searchBar)
}
}
// this delegate method is called when cell is tapped
// remember to connect delegate from storyboard or you can use self.tableView.delegate = self
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //here get the text from tapped cell index
    let tappedCellText = areaNameArr[indexPath.row] as? String
    // here set placeholder
    searchBar.placeholder = tappedCellText
    // when cell is clicked you need to set text of search bar to nil or blank otherwise you would not be able to see placeholder text because some text is already there in search bar
     searchBar.text = ""
    // if you don't cleanup text from search bar you will be required to delete text from search bar manually to see placeholder text
}

如果这对您没有帮助,请告诉我