gpt4 book ai didi

ios - UISearchBar 打开第二个 TableView 没有结果

转载 作者:行者123 更新时间:2023-11-28 13:58:14 25 4
gpt4 key购买 nike

我有一个由 Label、Button、SearchBar 和 TableView 组成的 ViewController。

这是 View 的样子,以及它的流程(以及问题)。

Layouts

我有两个 View 的导出

@IBOutlet weak var _searchBar: UISearchBar!
@IBOutlet weak var _tableView: UITableView!
// ...
self._searchBar.delegate = self

搜索完成后,我会在此处捕获它:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let query = searchBar.text {
print("Search query: \(query)")
//Fetch data from CoreData and update datasource array
self._tableView.reloadData()
}
}

我使用 NSPredicate 从 Core Data 获取数据,而不是过滤已经存在的数据。

获取数据工作正常,但是当输入任何内容时,我会看到另一个只显示“无结果”的 TableView,即使搜索正在找到数据也是如此。我可以单击“取消”,然后我可以看到在普通 TableView 中获取的数据。

如何让这些数据显示在自动打开的TableView中?

我还尝试将 SearchBar 指定为 TableViewHeaderView,但这让一切都消失了。

最佳答案

不确定你是否已经这样做了,但你需要实现另一个 UISearchBarDelegate 方法:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// Only update the top / Search Results table view here...
}

如果您可以提供此功能的完整代码,我们可以给您更好的反馈。

这是有效的解决方案(记住,searchController 应该是一个实例变量,而不是本地变量):

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search for products or activities"
searchController.searchBar.sizeToFit()
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.showsScopeBar = true
searchController.searchBar.tintColor = UIColor.colorPrimary()
searchController.searchBar.barTintColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
searchController.searchBar.scopeButtonTitles = ["Products", "Activities"]
searchController.searchBar.delegate = self
searchController.searchBar.showsCancelButton = true
self.definesPresentationContext = true

// Prepare our tableView
self._tableView.rowHeight = UITableView.automaticDimension
self._tableView.separatorStyle = .none
self._tableView.showsVerticalScrollIndicator = false
self._tableView.delegate = self
self._tableView.dataSource = self
self._tableView.register(ProductViewCell.nib, forCellReuseIdentifier: ProductViewCell.identifier)
self._tableView.register(ActivityViewCell.nib, forCellReuseIdentifier: ActivityViewCell.identifier)
self._tableView.tableHeaderView = searchController.searchBar
}

// MARK: UITableViewDataSource Methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return NumberOfRows
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Return a Result Cell
}
}


extension SearchViewController: UISearchBarDelegate, UISearchResultsUpdating {
// MARK: UISearchControllerDelegate
public func didPresentSearchController(_ searchController: UISearchController) {
searchController.searchBar.becomeFirstResponder()
}

// MARK: UISearchResultsUpdating
public func updateSearchResults(for searchController: UISearchController) {
if let query = searchController.searchBar.text {
print("Search query: \(query)")
//Fetch data from CoreData and update datasource array
fetchData()
self._tableView.reloadData()
}
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("search")
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print(searchText)
}
}

关于ios - UISearchBar 打开第二个 TableView 没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53745597/

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