UISearchController没有更新

时间:2015-03-28 14:12:18

标签: xcode swift ios8 uisearchcontroller

我在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()   
    }
}

3 个答案:

答案 0 :(得分:2)

正如Praveen Gowda IV所说,你想将searchResultsControllerself更改为nil。您还需要声明searchcon viewDidLoad以外的var searchcon = UISearchController(searchResultsController: nil) override func viewDidLoad() { // your implementation }

updateSearchResultsForSearchController

但是,您获得的行为可能不是您想要的,因为在搜索之前会调用{{1}}:

  

当搜索栏成为第一个响应者或时间时调用   用户在搜索栏内进行更改。 (Apple

答案 1 :(得分:0)

正如我在评论中建议的那样,您需要在self的初始化中将null更改为UISearchController

override func viewDidLoad() {

    super.viewDidLoad()

    var searchcon = UISearchController(searchResultsController: nil)
    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

}

即使在更改文本之前,有关updateSearchResultsForSearchController被调用的user3694363所说的内容也是正确的。但你可以遵循以下技巧,以防止它成为第一个响应者时做任何事情

func updateSearchResultsForSearchController(searchController: UISearchController) {

        // No need to update anything if we're being dismissed.
        if !searchController.active {
            return
        }

        // do the filtering here
    }

答案 2 :(得分:-1)

尝试使用其他内容的代码。

class searchresult: UITableViewController {      

var searchcon = UISearchController(searchResultsController: nil)

searchcon.searchResultsUpdater = self
searchcon.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchcon.searchBar
searchcon.searchBar.showsSearchResultsButton = true
self.definesPresentationContext = true
searchcon.dimsBackgroundDuringPresentation = false
}