2

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

3 回答 3

2

正如 Praveen Gowda IV 所说,您想将searchResultsControllerfrom更改selfnil. 您还需要声明您的searchcon外部viewDidLoad

var searchcon = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    // your implementation
}

但是,您得到的行为可能不是您想要的,因为updateSearchResultsForSearchController在搜索之前被调用:

当搜索栏成为第一响应者或用户在搜索栏中进行更改时调用。(苹果

于 2015-03-28T16:34:38.870 回答
0

正如我在评论中建议的那样,您self需要nullUISearchController.

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

}

user3694363 所说的updateSearchResultsForSearchController甚至在文本更改之前就被调用是正确的。但是你可以按照下面的技巧来防止当它成为第一响应者时做任何事情

func updateSearchResultsForSearchController(searchController: UISearchController) {

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

        // do the filtering here
    }
于 2015-03-28T17:52:11.513 回答
-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
}
于 2015-03-28T15:56:20.420 回答