这在 swift 4 中是如何实现的?
How is this implemented in swift 4?
我有这个 用于 table 视图中的下拉搜索栏。我能够使用此代码
在 table 视图中实现搜索栏
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = ""
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.tableView.tableHeaderView = searchController.searchBar
}
definesPresentationContext = true
searchController.searchBar.delegate = self
我现在想实现搜索功能,但是我无法在搜索栏中获取文本值。任何人都可以提供有关如何正确实施的任何提示吗?谢谢你。
编辑:
这是视图显示时隐藏搜索栏的 viewWillAppear 部分。我现在有另一个问题。如果我开始编辑搜索栏,它就会完全从视图中隐藏起来。如果我删除 searchController.searchBar.delegate = self
那么搜索栏将不会隐藏。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.contentOffset = CGPoint(x: 0,y :60.0)
}
我建议使用 UISearchResultsUpdating
协议而不是设置 searchController.searchBar.delegate = self
。
import UIKit
class MyViewController: UIViewController, UISearchResultsUpdating {
override func viewDidLoad() {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = ""
searchController.hidesNavigationBarDuringPresentation = false
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.tableView.tableHeaderView = searchController.searchBar
}
definesPresentationContext = true
//searchController.searchBar.delegate = self <--- you don't need this
}
func updateSearchResults(for searchController: UISearchController) {
if let searchString = searchController.searchBar.text {
print(searchString)
}
}
}
我有这个
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = ""
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.tableView.tableHeaderView = searchController.searchBar
}
definesPresentationContext = true
searchController.searchBar.delegate = self
我现在想实现搜索功能,但是我无法在搜索栏中获取文本值。任何人都可以提供有关如何正确实施的任何提示吗?谢谢你。
编辑:
这是视图显示时隐藏搜索栏的 viewWillAppear 部分。我现在有另一个问题。如果我开始编辑搜索栏,它就会完全从视图中隐藏起来。如果我删除 searchController.searchBar.delegate = self
那么搜索栏将不会隐藏。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.contentOffset = CGPoint(x: 0,y :60.0)
}
我建议使用 UISearchResultsUpdating
协议而不是设置 searchController.searchBar.delegate = self
。
import UIKit
class MyViewController: UIViewController, UISearchResultsUpdating {
override func viewDidLoad() {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = ""
searchController.hidesNavigationBarDuringPresentation = false
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.tableView.tableHeaderView = searchController.searchBar
}
definesPresentationContext = true
//searchController.searchBar.delegate = self <--- you don't need this
}
func updateSearchResults(for searchController: UISearchController) {
if let searchString = searchController.searchBar.text {
print(searchString)
}
}
}