我正在尝试使用UISearchBar
编写简单的UITableView
。当我拖放一个UITableViewController
时,我没有问题。一切正常! (见下面的代码)iOS中未调用updateSearchResultsForSearchController 9
override func viewDidLoad() {
super.viewDidLoad()
self.searchResultsController = UISearchController(searchResultsController: nil) //initialize the search controller
self.searchResultsController.searchResultsUpdater = self //the search controller updater is this view
self.searchResultsController.dimsBackgroundDuringPresentation = false
self.searchResultsController.searchBar.sizeToFit()
self.searchResultsController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchResultsController.searchBar.showsCancelButton = true
self.tableView.tableHeaderView = searchResultsController.searchBar
self.tableView.reloadData()
}
但现在,为了实验的目的,我用UIViewController
,并且加入了UITableView
,没有问题显示在表我的记录。
然后,从故事板中添加一个UISearchBar,将其委托设置为UIViewController,但当用户输入某些东西时,不调用updateSearchResultsForSearchController
方法。
这就像我的UISearchController不知道有一个UISearchBar
,并且我输入什么,并没有唤起更新方法。我必须告诉UISearchController,嘿,这是你的UISearchBar
?
所以这里是顶部的我的代码:
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate
{
@IBOutlet weak var mySearchBar: UISearchBar!
@IBOutlet weak var myViewTable: UITableView!
let allElements = ["H", "Li", "Na", "K", "Rb", "Cs", "Fr"]
var filteredElemetns = [String]()
var searchResultsController = UISearchController() //create a new search controller
override func viewDidLoad()
{
super.viewDidLoad()
self.searchResultsController = UISearchController(searchResultsController: nil) //initialize the search controller
self.searchResultsController.searchResultsUpdater = self //the search controller updater is this view
self.searchResultsController.dimsBackgroundDuringPresentation = false
self.searchResultsController.searchBar.sizeToFit()
self.searchResultsController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchResultsController.searchBar.delegate = self
//self.myViewTable.tableHeaderView = self.searchResultsController.searchBar
}
如果我去掉最后一行,那么我将有两个UISearchBar
,其中之一是由故事板和补充另一个用最后一行代码。我添加的那个,不起作用,但在myViewTable
的顶部。
你的类是否符合UISearchResultsUpdating,UISearchControllerDelegate协议? – MDB983
missing searchController.searchBar.delegate = self? –
是的,它符合。 –