作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我目前正在尝试为应用程序实现搜索功能。目标是在不同的 View Controller 中显示搜索结果。我的意思是:
UISearchController(searchResultsController: myCustomViewControllerHere)
我在教程中找到的所有答案都使用带有数据的当前 View Controller 作为显示结果的 View Controller :
UISearchController(searchResultsController: nil)
有人可以帮忙解决这个问题吗?我正在链接一个教程,它在同一 VC 上显示结果
最佳答案
您需要创建一个 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
}
}
希望对您有所帮助。
关于ios - 如何在 UISearchController 中使用自定义 View Controller 获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48569818/
我是一名优秀的程序员,十分优秀!