gpt4 book ai didi

ios - 搜索栏故障

转载 作者:行者123 更新时间:2023-11-28 15:29:45 26 4
gpt4 key购买 nike

问题:

我没有将搜索栏(带有搜索和结果 Controller )添加到 TableView Controller ,而是将其添加到常规 View Controller 的导航栏。一开始一切似乎都很好,但是当我点击搜索栏时,屏幕变灰了。

这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate{

var schools = ["Saratoga", "Fremont", "Argonaut", "Redwood", "Foothill", "Miller", "Rolling Hills"].sorted()
var filteredSchools = ["Saratoga", "Fremont", "Argonaut", "Redwood", "Foothill", "Miller", "Rolling Hills"].sorted()

var searchController: UISearchController!
var resultsController: UITableViewController!

override func viewDidLoad() {
super.viewDidLoad()

resultsController = UITableViewController()
searchController = UISearchController(searchResultsController: resultsController)

resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
searchController.searchResultsUpdater = self

self.view.addSubview(searchController.searchBar)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: searchController.searchBar)
}

func updateSearchResults(for searchController: UISearchController) {
let currText = searchController.searchBar.text ?? ""
filteredSchools = schools.filter({ (school) -> Bool in
if school.contains(currText){
return true
}
return false
})
resultsController.tableView.reloadData()
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = filteredSchools[indexPath.row]
return cell
}

}

最佳答案

viewDidLoad 中添加这些行:

resultsController.tableView.backgroundColor = UIColor.clear
searchController.hidesNavigationBarDuringPresentation = false

你的导航栏被隐藏了,仅此而已。

如果您不想要灰色调:

searchController.dimsBackgroundDuringPresentation = false

关于ios - 搜索栏故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44854117/

26 4 0