gpt4 book ai didi

ios - 如何在 UISearchController 中使用自定义 View Controller 获取结果?

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:03 27 4
gpt4 key购买 nike

我目前正在尝试为应用程序实现搜索功能。目标是在不同的 View Controller 中显示搜索结果。我的意思是:

UISearchController(searchResultsController: myCustomViewControllerHere)

我在教程中找到的所有答案都使用带有数据的当前 View Controller 作为显示结果的 View Controller :

UISearchController(searchResultsController: nil)

有人可以帮忙解决这个问题吗?我正在链接一个教程,它在同一 VC 上显示结果

Tutorial Link

最佳答案

您需要创建一个 SearchController 并将其设置为在另一个 TableViewController 中显示结果:

View Controller

import UIKit

class ViewController: UIViewController, UISearchControllerDelegate {

var controladorDeBusca: UISearchController!

var resultsTableViewController: ResultsTableViewController?

override func viewDidLoad() {
super.viewDidLoad()

resultsTableViewController = storyboard!.instantiateViewController(withIdentifier: "resultsTableViewController") as? ResultsTableViewController

configurarControladorDeBusca()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func configurarControladorDeBusca() {

controladorDeBusca = UISearchController(searchResultsController: resultsTableViewController)
controladorDeBusca.delegate = self
controladorDeBusca.searchResultsUpdater = resultsTableViewController
controladorDeBusca.dimsBackgroundDuringPresentation = true
definesPresentationContext = true

controladorDeBusca.loadViewIfNeeded()

//Configura a barra do Controlador de busca
controladorDeBusca.searchBar.delegate = resultsTableViewController
controladorDeBusca.hidesNavigationBarDuringPresentation = false
controladorDeBusca.searchBar.placeholder = "Search place"
controladorDeBusca.searchBar.sizeToFit()
controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
controladorDeBusca.searchBar.tintColor = self.view.tintColor

//Adiciona a barra do Controlador de Busca a barra do navegador
navigationItem.titleView = controladorDeBusca.searchBar
}
}

结果 TableView Controller

import UIKit

class ResultsTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

var array = ["Brazil", "Bolivia", "United States", "Canada", "England", "Germany", "France", "Portugal"]

var arrayFilter = [String]()

override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return arrayFilter.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)

cell.textLabel?.text = arrayFilter[indexPath.row]

return cell
}


func updateSearchResults(for searchController: UISearchController) {


}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

arrayFilter.removeAll()

if let text = searchBar.text {
for string in array {
if string.contains(text) {
arrayFilter.append(string)
}
}
}

tableView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

arrayFilter.removeAll()
tableView.reloadData()
}

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

arrayFilter.removeAll()
tableView.reloadData()

return true
}
}

希望对您有所帮助。

enter image description here

关于ios - 如何在 UISearchController 中使用自定义 View Controller 获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48569818/

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