gpt4 book ai didi

ios - 单击指定单元格后获取搜索栏占位符中的 TableView 单元格文本值

转载 作者:行者123 更新时间:2023-11-30 11:54:07 28 4
gpt4 key购买 nike

我的ViewController包含一个表格 View 。我正在从 JSON 获取数据,并且已经能够在我的表格 View 单元格中显示该值。还有用于过滤目的的搜索栏

我想将我的表格 View 单元格文本作为我的搜索栏
单击该特定单元格时的占位符

Showing each cell values

我希望在单击该特定单元格时将 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!
}

最佳答案

您需要将 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
}

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

关于ios - 单击指定单元格后获取搜索栏占位符中的 TableView 单元格文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022743/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com