我在 ios8 和 swift 中使用 xcode 6.2 进行开发,我试图在 UITableViewController 中实现一个 UISearchController 可以在搜索后更新,我写了一些简单的代码来检查它是否有效,但是当我编译时,搜索栏是在那里,但是当我输入文本并点击搜索时,什么也没有发生:
这是我的代码:
import UIKit
class searchresult: UITableViewController,UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
var result:[String] = ["a","b","c"]
override func viewDidLoad() {
super.viewDidLoad()
var searchcon = UISearchController(searchResultsController: self)
searchcon.delegate = self
searchcon.searchBar.delegate = self
searchcon.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchcon.searchBar
searchcon.searchResultsUpdater = self
searchcon.searchBar.showsSearchResultsButton = true
self.definesPresentationContext = true
searchcon.dimsBackgroundDuringPresentation = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel!.text = result[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return result.count
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
println("updating")
result = ["1"]
self.tableView.reloadData()
}
}