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

标签 ios swift uitableview uisearchbar

我的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/

相关文章:

image - 通过在自定义单元格内进行修饰来制作基本的动画/翻译

iphone - 如何阻止 UITable 在减速时覆盖我设置的内容偏移量?

ios - Xcode 10 Swift 构建错误 : "Converting non-escaping value to ' T' may allow it to escape"

ios - Xcode 模拟器替代方案

ios - 从 Keychain 检索数据时,一小部分用户会收到 errSecItemNotFound

ios - 如何将 Date 转换为 TimeInterval 以便可以使用 if 语句

iphone - 应用程序在启动时崩溃非常罕见

ios - 在 swift 4 中返回之前强制等待 Alamofire 响应

ios - 我如何使用在 main.storyboard 中创建的原型(prototype)单元格数组填充正确的 swreveal View Controller

ios - 如何获取表格单元格中的标签以继续到下一行而不是被切断屏幕? (Xcode 8)