如何在点击回调时访问搜索栏

问题描述

我正在构建一个应用程序,我有一个带有表格视图的搜索栏。 但我不知道当用户点击搜索时,如何转到不同视图控制器的数据 有人可以帮我吗? 我的代码差不多就是这样

@IBOutlet weak var textSearchBar: UITextField!
@IBOutlet weak var tableSearchResult: UITableView!

var fruitsArray:[String] = Array()
var searchedArray:[String] = Array()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    fruitsArray.append("Apple")
    fruitsArray.append("Orange")
    fruitsArray.append("Litch")
    fruitsArray.append("Pineapple")
    
    for str in fruitsArray {
        searchedArray.append(str)
    }
    tableSearchResult.dataSource = self
    textSearchBar.delegate = self
}

// Mark:- UITableViewDataSource

func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return searchedArray.count
}


func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    
    if cell == nil {
        cell = UITableViewCell(style: .default,reuseIdentifier: "cell")
        
    }
    
    cell?.textLabel?.text = searchedArray [indexPath.row]
    return cell!
}

    // MARK: - UITextFieldDelegate

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    textSearchBar.resignFirstResponder()
    textSearchBar.text = ""
    self.searchedArray.removeAll()
    for str in fruitsArray {
        searchedArray.append(str)
    }
    tableSearchResult.reloadData()
    return true
}

非常感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)