SearchBar委托在UISearchController不工作
问题描述:
我想添加一个搜索栏到一个视图,但我试过了它的一千种方法和搜索栏委托不起作用。SearchBar委托在UISearchController不工作
class SearchTableViewController: UITableViewController, UISearchBarDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = true
searchController.obscuresBackgroundDuringPresentation = true
self.definesPresentationContext = true
searchController.searchBar.delegate = self
self.tableView.tableHeaderView = searchController.searchBar
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
答
我遇到了同样的问题。
您遇到的问题是,UITableViewController中有一个很大的限制:其主要观点必须是一个UITableView,占用了整个屏幕空间(除了在顶部有一个可能的导航栏,工具栏或标签栏在底部)。
如果你的屏幕只包含一个UITableView,那么使用它就很有意义。但是,由于您正在添加搜索栏,因此您必须使用UIViewController并向其中添加UITableView。然后,您可以随意添加搜索栏。
无论如何,我试过它,它不是委托,UISearchBarDelegate协议失败不起作用,我在UIViewController中试过它,没有任何东西。 – luistejadaa